<?php

namespace SugarCalendarBookings\Customers\Admin;

use Exception;
use SugarCalendarBookings\Plugin;
use SugarCalendarBookings\Customers\CustomerDTO;
use SugarCalendarBookings\Customers\CustomerModel;
use SugarCalendarBookings\Customers\CustomerService;
use SugarCalendarBookings\Customers\CreateCustomerDTO;
use SugarCalendarBookings\Support\Ajax\AbstractController;

/**
 * Controller for Customer admin actions.
 *
 * @since 1.0.0
 */
class CustomerController extends AbstractController {

	/**
	 * Handle customer creation request.
	 *
	 * @since 1.0.0
	 */
	public function create() {

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( esc_html__( 'You are not allowed to create customer.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$customer_form = Plugin::get( CustomerFormService::class );
		$data          = $this->get_input_data( $customer_form->rules() );

		$dto = new CreateCustomerDTO( $data );

		try {
			$customer = Plugin::get( CustomerService::class )->create( $dto );
		} catch ( Exception $e ) {
			wp_send_json_error( $e->getMessage() );
		}

		wp_send_json_success(
			[
				'id'   => $customer->id,
				'name' => $customer->name,
			]
		);
	}

	/**
	 * Handle customer update request.
	 *
	 * @since 1.0.0
	 */
	public function update() {

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( esc_html__( 'You are not allowed to update customer.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$customer_form = Plugin::get( CustomerFormService::class );

		$rules = $customer_form->rules(
			[
				'id' => 'required|id',
			]
		);

		$data     = $this->get_input_data( $rules );
		$customer = CustomerModel::get( $data['id'] );

		if ( ! $customer ) {
			wp_send_json_error( esc_html__( 'Customer not found.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$dto = new CustomerDTO( $data );

		try {
			$customer = Plugin::get( CustomerService::class )->update( $customer, $dto );
		} catch ( Exception $e ) {
			wp_send_json_error( $e->getMessage() );
		}

		wp_send_json_success(
			[
				'id'   => $customer->id,
				'name' => $customer->name,
			]
		);
	}

	/**
	 * Handle customer deletion request.
	 *
	 * @since 1.0.0
	 */
	public function delete() {

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( esc_html__( 'You are not allowed to delete customer.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$rules = [
			'id' => 'required|id',
		];

		$data     = $this->get_input_data( $rules );
		$customer = CustomerModel::get( $data['id'] );

		if ( ! $customer ) {
			wp_send_json_error( esc_html__( 'Customer not found.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$deleted = Plugin::get( CustomerService::class )->delete( $customer );

		if ( ! $deleted ) {
			wp_send_json_error( esc_html__( 'Failed to delete customer.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		wp_send_json_success();
	}

	/**
	 * Get data for customer creation form.
	 *
	 * @since 1.0.0
	 */
	public function get_create_form_data() {

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( esc_html__( 'You are not allowed to create customer.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$data = Plugin::get( CustomerFormService::class )->get_create_form_data();

		wp_send_json_success( $data );
	}

	/**
	 * Get data for customer edit form.
	 *
	 * @since 1.0.0
	 */
	public function get_edit_form_data() {

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( esc_html__( 'You are not allowed to edit customer.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$rules = [
			'id' => 'required|id',
		];

		$data     = $this->get_input_data( $rules );
		$customer = CustomerModel::get( $data['id'] );

		if ( ! $customer ) {
			wp_send_json_error( esc_html__( 'Customer not found.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$data = Plugin::get( CustomerFormService::class )->get_edit_form_data( $customer );

		wp_send_json_success( $data );
	}
}
