<?php

namespace forms\objects;

use forms\form_helper_c;
use objects\bookings_o;
use traits\form_t;

class complete_booking_f extends form_helper_c
{
    use form_t;

    /**
     * @inheritDoc
     */
    public function sanitize(): void
    {
        foreach ( $this->form_unsanitized_data as $key => $value ) {
            // Sanitize the input TODO: Implement the sanitization logic
            $this->form_unsanitized_data[$key] = $value;
        }
        // Set the sanitized data
        $this->form_sanitized_data = $this->form_unsanitized_data;
    }

    /**
     * @inheritDoc
     */
    public function validateInput(): void
    {

    }

    /**
     * @inheritDoc
     */
    public function beforeSave(): void
    {
        // Get the booking from the booking id
        $bookings = new bookings_o();
        $bookings->select(self::getSanitizedData('booking_id'));
        // Check if the user has access to the booking
        self::restrictAccessDepartment($bookings->department->value());
        // Check if the user has access to issue wash certificates
        self::requirePermission(
            'issue_wash_certificates',
            'User does not have access to issue wash certificates',
        );
        // Set the department id to the department id of the user
        self::setDepartmentId($bookings->department->value());
        // Set the customer number to the customer number of the user
        self::setCustomerNumber($bookings->customer_number->value());
        // Check if the booking is cancelled
        switch ($bookings->status->value()) {
            case 'cancelled':
                throw new \Exception('Booking is cancelled');
                break;
            case 'completed':
                throw new \Exception('Booking is completed');
                break;
            case 'pending':
                break;
            default:
                throw new \Exception('Booking status is unknown, expected cancelled, completed or pending');
                break;
        }
        // Generate the wash certificate
        $bookings->completeWashWithoutWashCertificate(
            $bookings->id,
        );
    }

    /**
     * @inheritDoc
     */
    public function afterSubmit(): void
    {
        // TODO: Implement afterSubmit() method.
    }

    /**
     * @inheritDoc
     */
    public function setup(): void
    {
        self::setFormIdentifier('COMPLETE_BOOKING_WITHOUT_WASH_CERTIFICATE');
        self::setFormName('Bekræft vask');
        self::setFormDescription('Bekræft vask af booking');
        self::setSubmitButtonText('Bekræft vask');
        // Set the form fields
        self::defineInputFieldsAdvanced([
            'booking_id' => [
                'description' => 'Booking ID er det unikke ID for den booking, du vil oprette et vaskecertifikat til.',
                'required' => true,
                'placeholder' => 'Indtast booking ID',
                'label' => 'Booking ID',
                'help' => 'Dette er det unikke ID for den booking, du vil oprette et vaskecertifikat til.',
                'error' => 'Du skal indtaste et gyldigt booking ID.',
                'validation_method' => 'validateBookingId',
            ],
        ]);
    }
}