<?php

namespace HelpGent\App\Http\Controllers\Admin;

defined( 'ABSPATH' ) || exit;

use HelpGent\App\Repositories\UserRepository;
use HelpGent\WpMVC\RequestValidator\Validator;
use HelpGent\App\Http\Controllers\Controller;
use HelpGent\WpMVC\Routing\Response;
use WP_REST_Request;

class UserController extends Controller {
    public UserRepository $repository;

    public function __construct( UserRepository $repository ) {
        $this->repository = $repository;
    }

    public function users( Validator $validator, WP_REST_Request $wp_rest_request ) {
        $validator->validate(
            [
                'per_page' => 'numeric',
                'page'     => 'numeric'
            ]
        );

        $response = $this->repository->get(
            intval( $wp_rest_request->get_param( 'per_page' ) ),
            intval( $wp_rest_request->get_param( 'page' ) )
        );

        return Response::send( $response );
    }

    public function export_users() {
        return Response::send(
            [
                'csv_path' => $this->repository->export(),
                'message'  => esc_html__( 'The user list has been successfully exported.', 'helpgent' )
            ]
        );
    }
}