<?php

namespace forms\objects;

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

class generate_booking_wash_certificate_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;
        }
        try {
            $safety_seal = (int)self::getSanitizedData('safety_seal');
        } catch (\Exception $e) {
            $safety_seal = null;
        }
        try {
            $operator = (string)self::getSanitizedData('operator');
        } catch (\Exception $e) {
            $operator = null;
        }
        // Generate the wash certificate
        $bookings->generateWashCertificate(
            $safety_seal,
            $operator,
        );
        // Send the wash certificate to the customer
        $bookings->sendWashCertificateToCustomer();
        // Create the transaction based on the booking
        //$transaction = $bookings->createTransaction();
        // Mark the booking as completed
        $bookings->status->set('completed');
        $bookings->washCertificateStatus->set('completed');
    }

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

    /**
     * @inheritDoc
     */
    public function setup(): void
    {
        self::setFormIdentifier('GENERATE_BOOKING_WASH_CERTIFICATE');
        self::setFormName('Opret vaskecertifikat');
        self::setFormDescription('Du kan oprette et vaskecertifikat til en vask, der allerede er booket. Du skal blot indtaste de nødvendige oplysninger nedenfor.');
        self::setSubmitButtonText('Opret vaskecertifikat');
        // 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',
            ],
            'safety_seal' => [
                'description' => 'En sikkerhedssikring er en form for beskyttelse, der sikrer, at køretøjet ikke er blevet åbnet eller ændret efter vasken.',
                'required' => false,
                'placeholder' => 'Indtast sikkerhedssikring',
                'label' => 'Safety Seal / PLOM',
                'help' => 'Dette er et Safety Seal, der bruges til at dokumentere, at køretøjet er blevet vasket.',
                'error' => 'Du skal indtaste en gyldig sikkerhedssikring.',
                'validation_method' => 'validateInt',
                'default' => '',
            ],
            'operator' => [
                'description' => 'Operatøren er den person, der har vasket køretøjet. Dette felt er valgfrit.',
                'required' => false,
                'placeholder' => 'Indtast operatør',
                'label' => 'Vognvasker',
                'help' => 'Dette er navnet på den person, der har vasket køretøjet.',
                'error' => 'Du skal indtaste en gyldig operatør.',
                'validation_method' => 'validateString',
                'default' => '',
            ],
        ]);

    }
}