<?php

namespace WeDevs\ERP\HRM\API;

use WeDevs\ERP\API\REST_Controller;
use WP_REST_Response;
use WP_REST_Server;

class CompanyController extends REST_Controller {

    /**
     * Endpoint namespace.
     *
     * @var string
     */
    protected $namespace = 'erp/v1';

    /**
     * Route base.
     *
     * @var string
     */
    protected $rest_base = 'hrm/company';

    /**
     * Register the routes for the objects of the controller.
     */
    public function register_routes() {
        register_rest_route( $this->namespace, '/' . $this->rest_base . '/company-locations', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_company_locations' ],
                'args'                => $this->get_collection_params(),
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_view_list' );
                },
            ],
            'schema' => [ $this, 'get_public_item_schema' ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/employee-statuses', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_employee_statuses' ],
                'args'                => $this->get_collection_params(),
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_list_employee' );
                },
            ],
            'schema' => [ $this, 'get_public_item_schema' ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/employee-types', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_employee_types' ],
                'args'                => $this->get_collection_params(),
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_view_list' );
                },
            ],
            'schema' => [ $this, 'get_public_item_schema' ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/pay-types', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_employee_pay_types' ],
                'args'                => $this->get_collection_params(),
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_view_list' );
                },
            ],
            'schema' => [ $this, 'get_public_item_schema' ],
        ] );
        register_rest_route( $this->namespace, '/' . $this->rest_base . '/hiring-sources', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_source_of_hire' ],
                'args'                => $this->get_collection_params(),
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_view_list' );
                },
            ],
            'schema' => [ $this, 'get_public_item_schema' ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/pay-change-reasons', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_pay_change_reasons' ],
                'args'                => $this->get_collection_params(),
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_view_list' );
                },
            ],
            'schema' => [ $this, 'get_public_item_schema' ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/termination-types', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_termination_types' ],
                'args'                => $this->get_collection_params(),
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_view_list' );
                },
            ],
            'schema' => [ $this, 'get_public_item_schema' ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/termination-reasons', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_termination_reasons' ],
                'args'                => $this->get_collection_params(),
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_view_list' );
                },
            ],
            'schema' => [ $this, 'get_public_item_schema' ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/rehire-options', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_rehire_options' ],
                'args'                => $this->get_collection_params(),
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_view_list' );
                },
            ],
            'schema' => [ $this, 'get_public_item_schema' ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/genders', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_genders' ],
                'permission_callback' => '__return_true',
            ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/marital-statuses', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_marital_statuses' ],
                'permission_callback' => '__return_true',
            ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/education-result-types', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_education_result_types' ],
                'permission_callback' => '__return_true',
            ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/performance-ratings', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_performance_ratings' ],
                'permission_callback' => '__return_true',
            ],
        ] );

        register_rest_route( $this->namespace, '/' . $this->rest_base . '/work-days', [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'get_work_days' ],
                'permission_callback' => function ( $request ) {
                    return current_user_can( 'erp_view_list' );
                },
            ],
        ] );
    }

    /**
     * Get company locations
     *
     * @since 1.3.2
     *
     * @param $request
     *
     * @return mixed|object|WP_REST_Response
     */
    public function get_company_locations( $request ) {
        $locations = erp_company_get_locations();
        $response  = rest_ensure_response( $locations );
        $response  = $this->format_collection_response( $response, $request, count( $locations ) );

        return $response;
    }

    /**
     * Get employee statuses
     *
     * @since 1.3.2
     *
     * @param $request
     *
     * @return mixed|object|WP_REST_Response
     */
    public function get_employee_statuses( $request ) {
        $statuses = erp_hr_get_employee_statuses();
        $response = rest_ensure_response( $statuses );
        $response = $this->format_collection_response( $response, $request, count( $statuses ) );

        return $response;
    }

    /**
     * Get employee pay types
     *
     * @since 1.3.2
     *
     * @param $request
     *
     * @return mixed|object|WP_REST_Response
     */
    public function get_employee_pay_types( $request ) {
        $pay_types = erp_hr_get_pay_type();
        $response  = rest_ensure_response( $pay_types );
        $response  = $this->format_collection_response( $response, $request, count( $pay_types ) );

        return $response;
    }

    /**
     * Get employee types
     *
     * @since 1.3.2
     *
     * @param $request
     *
     * @return mixed|object|WP_REST_Response
     */
    public function get_employee_types( $request ) {
        $employee_types = erp_hr_get_employee_types();
        $response       = rest_ensure_response( $employee_types );
        $response       = $this->format_collection_response( $response, $request, count( $employee_types ) );

        return $response;
    }

    /**
     * Get hiring source
     *
     * @since 1.3.2
     *
     * @param $request
     *
     * @return mixed|object|WP_REST_Response
     */
    public function get_source_of_hire( $request ) {
        $sources  = erp_hr_get_employee_sources();
        $response = rest_ensure_response( $sources );
        $response = $this->format_collection_response( $response, $request, count( $sources ) );

        return $response;
    }

    /**
     * Get pay change reasons
     *
     * @since 1.3.2
     *
     * @param $request
     *
     * @return mixed|object|WP_REST_Response
     */
    public function get_pay_change_reasons( $request ) {
        $reasons  = erp_hr_get_pay_change_reasons();
        $response = rest_ensure_response( $reasons );
        $response = $this->format_collection_response( $response, $request, count( $reasons ) );

        return $response;
    }

    /**
     * Get termination types
     *
     * @since 1.3.2
     *
     * @param $request
     *
     * @return mixed|object|WP_REST_Response
     */
    public function get_termination_types( $request ) {
        $types    = erp_hr_get_terminate_type();
        $response = rest_ensure_response( $types );
        $response = $this->format_collection_response( $response, $request, count( $types ) );

        return $response;
    }

    /**
     * Get termination reasons
     *
     * @since 1.3.2
     *
     * @param $request
     *
     * @return mixed|object|WP_REST_Response
     */
    public function get_termination_reasons( $request ) {
        $reasons  = erp_hr_get_terminate_reason();
        $response = rest_ensure_response( $reasons );
        $response = $this->format_collection_response( $response, $request, count( $reasons ) );

        return $response;
    }

    /**
     * Get rehire options
     *
     * @since 1.3.2
     *
     * @param $request
     *
     * @return mixed|object|WP_REST_Response
     */
    public function get_rehire_options( $request ) {
        $options  = erp_hr_get_terminate_rehire_options();
        $response = rest_ensure_response( $options );
        $response = $this->format_collection_response( $response, $request, count( $options ) );

        return $response;
    }

    /**
     * Get the company weekly work-day schedule.
     *
     * Mirrors WP ERP HR settings → Work Days. Returns the raw per-weekday hours
     * (0 = non-working) plus derived state labels and working/weekend lists so
     * clients (e.g. the mobile app) can render the schedule without re-deriving
     * the full/half/off mapping themselves.
     *
     * @param \WP_REST_Request $request
     *
     * @return WP_REST_Response
     */
    public function get_work_days( $request ) {
        $hours = erp_hr_get_work_days();

        $labels       = [];
        $working_days = [];
        $weekends     = [];

        foreach ( $hours as $day => $value ) {
            $value = (int) $value;

            if ( 0 === $value ) {
                $labels[ $day ] = 'non_working';
                $weekends[]     = $day;
            } elseif ( $value >= 8 ) {
                $labels[ $day ] = 'full_day';
                $working_days[] = $day;
            } else {
                $labels[ $day ] = 'half_day';
                $working_days[] = $day;
            }
        }

        return rest_ensure_response(
            [
                'work_days'    => $hours,        // { mon: 8, ..., sat: 0, sun: 0 }
                'states'       => $labels,       // { mon: 'full_day', ..., sun: 'non_working' }
                'working_days' => $working_days, // [ 'mon', ..., 'fri' ]
                'weekends'     => $weekends,     // [ 'sat', 'sun' ]
            ]
        );
    }

    public function get_genders() {
        return rest_ensure_response( erp_hr_get_genders() );
    }

    public function get_marital_statuses() {
        return rest_ensure_response( erp_hr_get_marital_statuses() );
    }

    public function get_education_result_types() {
        return rest_ensure_response( erp_hr_get_education_result_type_options() );
    }

    public function get_performance_ratings() {
        return rest_ensure_response( erp_performance_rating() );
    }
}
