<?php

namespace SugarCalendarBookings\Locations\Admin;

use SugarCalendarBookings\Locations\CreateLocationDTO;
use SugarCalendarBookings\Locations\LocationDTO;
use SugarCalendarBookings\Locations\LocationModel;
use SugarCalendarBookings\Locations\LocationService;
use SugarCalendarBookings\Plugin;
use SugarCalendarBookings\Support\Ajax\AbstractController;

/**
 * Controller for Location admin actions.
 *
 * @since 1.0.0
 */
class LocationController extends AbstractController {

	/**
	 * Handle location 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 location.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$location_form = Plugin::get( LocationFormService::class );
		$data          = $this->get_input_data( $location_form->rules() );

		$dto      = new CreateLocationDTO( $data );
		$location = Plugin::get( LocationService::class )->create( $dto );

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

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

	/**
	 * Handle location 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 location.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

		$location_form = Plugin::get( LocationFormService::class );

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

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

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

		$dto      = new LocationDTO( $data );
		$location = Plugin::get( LocationService::class )->update( $location, $dto );

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

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

	/**
	 * Handle location 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 location.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

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

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

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

		$deleted = Plugin::get( LocationService::class )->delete( $location );

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

		wp_send_json_success();
	}

	/**
	 * Get data for location 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 location.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

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

		wp_send_json_success( $data );
	}

	/**
	 * Get data for location 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 location.', 'sugar-calendar-bookings-scheduling-appointments-lite' ) );
		}

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

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

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

		$data = Plugin::get( LocationFormService::class )->get_edit_form_data( $location );

		wp_send_json_success( $data );
	}
}
