<?php

namespace MasterStudy\Lms\Http\Controllers\Course;

use MasterStudy\Lms\Enums\PricingMode;
use MasterStudy\Lms\Http\WpResponseFactory;
use MasterStudy\Lms\Repositories\CourseRepository;
use MasterStudy\Lms\Repositories\PricingRepository;
use MasterStudy\Lms\Validation\Validator;
use WP_REST_Request;
use WP_REST_Response;

class UpdatePricingSettingsController {
	private CourseRepository $course_repository;
	private PricingRepository $pricing_repository;

	public function __construct() {
		$this->course_repository  = new CourseRepository();
		$this->pricing_repository = new PricingRepository();
	}

	public function __invoke( $course_id, WP_REST_Request $request ): WP_REST_Response {
		if ( ! $this->course_repository->exists( $course_id ) ) {
			return WpResponseFactory::not_found();
		}

		$validator = new Validator(
			$request->get_json_params(),
			array(
				// Free
				'free_do_not_provide_certificate'        => 'nullable|boolean',
				'free_paid_certificate'                  => 'nullable|boolean',
				'free_certificate_price'                 => 'required_if_accepted,free_paid_certificate|nullable|float',
				'free_price_info'                        => 'nullable|string',

				// Paid
				'single_sale'                            => 'nullable|boolean',
				'single_sale_price_info'                 => 'nullable|string',
				'single_sale_do_not_provide_certificate' => 'nullable|boolean',
				'single_sale_paid_certificate'           => 'required_if_accepted,single_sale|boolean',
				'single_sale_certificate_price'          => 'required_if_accepted,single_sale_paid_certificate|nullable|float',
				'price'                                  => 'required_if_accepted,single_sale|nullable|float',
				'sale_price'                             => 'nullable|float',
				'sale_price_dates_start'                 => 'required_with,sale_price_dates_end|nullable|integer',
				'sale_price_dates_end'                   => 'required_with,sale_price_dates_start|nullable|integer',

				'enterprise'                             => 'nullable|boolean',
				'enterprise_price'                       => 'required_if_accepted,enterprise|nullable|float',
				'enterprise_price_info'                  => 'nullable|string',
				'enterprise_do_not_provide_certificate'  => 'nullable|boolean',
				'enterprise_paid_certificate'            => 'required_if_accepted,enterprise|boolean',
				'enterprise_certificate_price'           => 'required_if_accepted,enterprise_paid_certificate|nullable|float',

				'not_membership'                         => 'nullable|boolean',
				'membership_price_info'                  => 'nullable|string',
				'membership_do_not_provide_certificate'  => 'nullable|boolean',
				'membership_paid_certificate'            => 'nullable|boolean',
				'membership_certificate_price'           => 'nullable|float',

				'points'                                 => 'nullable|boolean',
				'points_price'                           => 'nullable|float',
				'points_price_info'                      => 'nullable|string',
				'points_do_not_provide_certificate'      => 'nullable|boolean',
				'points_paid_certificate'                => 'nullable|boolean',
				'points_certificate_price'               => 'required_if_accepted,points_paid_certificate|nullable|float',

				'subscriptions'                          => 'nullable|boolean',
				'subscriptions_price_info'               => 'nullable|string',

				// Affiliate
				'affiliate_course'                       => 'nullable|boolean',
				'affiliate_course_text'                  => 'required_if_accepted,affiliate_course|string',
				'affiliate_course_link'                  => 'required_if_accepted,affiliate_course|string',
				'affiliate_course_price'                 => 'required_if_accepted,affiliate_course|float',

				'pricing_mode'                           => 'required|string|contains_list,' . implode( ';', PricingMode::cases() ),
			)
		);

		if ( $validator->fails() ) {
			return WpResponseFactory::validation_failed( $validator->get_errors_array() );
		}

		$data = $validator->get_validated();
		$this->pricing_repository->save( $course_id, $data );

		return WpResponseFactory::ok();
	}
}
