<?php

namespace Vendtics\App\Http\Controllers\Admin;

defined( "ABSPATH" ) || exit;

use Vendtics\App\Http\Controllers\Controller;
use Vendtics\WpMVC\Exceptions\Exception;
use Vendtics\WpMVC\Routing\Response;
use Vendtics\App\Repositories\ContactRepository;
use Vendtics\WpMVC\RequestValidator\Validator;
use WP_REST_Request;

class CountCampaignRecipientController extends Controller {
    public ContactRepository $contact_repository;

    public function __construct( ContactRepository $contact_repository ) {
        $this->contact_repository = $contact_repository;
    }

    /**
     * @param Validator $validator Instance of the Validator.
     * @param WP_REST_Request $request The REST request instance.
     * @return array
     */
    public function tags_lists( Validator $validator, WP_REST_Request $request ): array {
        $validator->validate(
            [
                "tags"  => "array",
                "lists" => "array",
            ]
        );

        $tags  = $request->has_param( 'tags' ) ? $request->get_param( 'tags' ) : [];
        $lists = $request->has_param( 'lists' ) ? $request->get_param( 'lists' ) : [];

        if ( ! vendtics_is_one_level_array( $tags ) || ! vendtics_is_one_level_array( $lists ) ) {
            throw new Exception( esc_html__( "Something was wrong", 'vendtics' ), 505 );
        }

        if ( empty( $tags ) && empty( $lists ) ) {
            $total = $this->contact_repository->total_subscriber();
        } else {
            $total = $this->contact_repository->count_by_tags_lists( $tags, $lists );
        }

        return Response::send(
            [
                'total' => $total
            ]
        );
    }

    /**
     * @param Validator $validator Instance of the Validator.
     * @param WP_REST_Request $request The REST request instance.
     * @return array
     */
    public function segment( Validator $validator, WP_REST_Request $request ): array {
        $validator->validate(
            [
                "segment_id" => "numeric"
            ]
        );

        if ( empty( $request->get_param( 'segment_id' ) ) ) {
            $total = $this->contact_repository->total_subscriber();
        } else {
            $total = $this->contact_repository->count_by_segment_id( absint( $request->get_param( 'segment_id' ) ) );
        }

        return Response::send(
            [
                'total' => $total
            ]
        );
    }

    /**
     * @param Validator $validator Instance of the Validator.
     * @param WP_REST_Request $request The REST request instance.
     * @return array
     */
    public function advanced_filter( Validator $validator, WP_REST_Request $request ): array {
        $validator->validate(
            [
                'queries' => 'json',
            ]
        );

        if ( empty( $request->get_param( 'queries' ) ) ) {
            $total = $this->contact_repository->total_subscriber();
        } else {
            $total = $this->contact_repository->count_by_advanced_filter( json_decode( $request->get_param( 'queries' ), true ) );
        }

        return Response::send(
            [
                'total' => $total
            ]
        );
    }
}