<?php

namespace HelpGent\App\Http\Controllers\Admin;

defined( 'ABSPATH' ) || exit;

use Exception;
use HelpGent\App\DTO\TagDTO;
use HelpGent\App\Http\Controllers\Controller;
use HelpGent\App\Repositories\TagRepository;
use HelpGent\WpMVC\RequestValidator\Validator;
use HelpGent\WpMVC\Routing\Response;
use WP_REST_Request;

class TagController extends Controller {
    public TagRepository $repository;

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

    public function index( Validator $validator, WP_REST_Request $wp_rest_request ) {
        $validator->validate(
            [
                'per_page' => 'numeric|min:1',
                'page'     => 'numeric|min:1',
                'search'   => 'string'
            ]
        );

        return Response::send(
            $this->repository->get(
                intval( $wp_rest_request->get_param( 'per_page' ) ),
                intval( $wp_rest_request->get_param( 'page' ) ),
                (string) $wp_rest_request->get_param( 'search' )
            )
        );
    }

    public function store( Validator $validator, WP_REST_Request $wp_rest_request ) {
        $validator->validate(
            [
                'title'   => 'required|string|max:255',
                'color'   => 'required|string|max:20',
                'form_id' => 'required|integer'
            ]
        );

        $form_id = intval( $wp_rest_request->get_param( 'form_id' ) );

        do_action( "helpgent_before_create_tag", $form_id, $wp_rest_request, $validator );

        $dto = ( new TagDTO() )
            ->set_title( $wp_rest_request->get_param( 'title' ) )
            ->set_color( $wp_rest_request->get_param( 'color' ) )
            ->set_form_id( $form_id )
            ->set_created_by( helpgent_get_current_user()->get_id() );

        $tag_id = $this->repository->create( $dto );

        return Response::send(
            [
                'tag_id'  => $tag_id,
                'message' => esc_html__( 'A new tag has been created successfully.', 'helpgent' )
            ]
        );
    }

    public function update( Validator $validator, WP_REST_Request $wp_rest_request ) {
        $validator->validate(
            [
                'id'      => 'required|numeric',
                'title'   => 'required|string|max:255',
                'color'   => 'required|string|max:20',
                'form_id' => 'required|integer'
            ]
        );

        $form_id = intval( $wp_rest_request->get_param( 'form_id' ) );

        do_action( "helpgent_before_update_tag", $form_id, $wp_rest_request, $validator );
        
        $dto = ( new TagDTO() )
            ->set_id( intval( $wp_rest_request->get_param( 'id' ) ) )
            ->set_title( $wp_rest_request->get_param( 'title' ) )
            ->set_color( $wp_rest_request->get_param( 'color' ) )
            ->set_form_id( $form_id );

        $this->repository->update( $dto );

        return Response::send(
            [
                'message' => esc_html__( 'The tag has been updated successfully.', 'helpgent' )
            ]
        );
    }

    public function delete( Validator $validator, WP_REST_Request $wp_rest_request ) {
        $validator->validate(
            [
                'id' => 'required|numeric',
            ]
        );

        $tag_id = $wp_rest_request->get_param( 'id' );

        do_action( "helpgent_before_delete_tag", $tag_id, $wp_rest_request, $validator );

        $delete = $this->repository->delete( $tag_id );

        if ( 0 === $delete ) {
            throw new Exception( esc_html__( 'Oops, something went wrong. Please try again.', 'helpgent' ), 500 );
        }

        return Response::send(
            [
                'message' => esc_html__( 'The tag has been deleted successfully.', 'helpgent' )
            ]
        );
    }
}