<?php

namespace App\Filament\Resources\QualityValidationResource\Pages;

use App\Filament\Resources\QualityValidationResource;
use App\Models\AlertMailRule;
use App\Models\Plant;
use Filament\Actions;
use Filament\Forms\Components\Component;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Validation\ValidationException;

class CreateQualityValidation extends CreateRecord
{
    protected static string $resource = QualityValidationResource::class;

    public $plantId;

    public function mount(): void
    {
        parent::mount();
        session()->forget([
            'last_selected_plant_id',
            'last_selected_line',
            'last_selected_production',
        ]);
    }

    protected function beforeCreate(): void
    {
        $errors = [];

        if (!empty($this->data['validationError'])) {
            $errors['validationError'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['pack_slip_motor_error'])) {
            $errors['pack_slip_motor_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['pack_slip_pump_error'])) {
            $errors['pack_slip_pump_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['pack_slip_pumpset_error'])) {
            $errors['pack_slip_pumpset_error'] = ['Fix the errors before submitting.'];
        }

        //..name plate

        if (!empty($this->data['name_plate_motor_error'])) {
            $errors['name_plate_motor_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['name_plate_pump_error'])) {
            $errors['name_plate_pump_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['name_plate_pumpset_error'])) {
            $errors['name_plate_pumpset_error'] = ['Fix the errors before submitting.'];
        }

        //..tube sticker

        if (!empty($this->data['tube_sticker_motor_error'])) {
            $errors['tube_sticker_motor_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['tube_sticker_pump_error'])) {
            $errors['tube_sticker_pump_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['tube_sticker_pumpset_error'])) {
            $errors['tube_sticker_pumpset_error'] = ['Fix the errors before submitting.'];
        }

        //..warranty
        if (!empty($this->data['warranty_card_error'])) {
            $errors['warranty_card_error'] = ['Fix the errors before submitting.'];
        }

        //..part validations

        if (!empty($this->data['part_validation1_error'])) {
            $errors['part_validation1_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['part_validation2_error'])) {
            $errors['part_validation2_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['part_validation3_error'])) {
            $errors['part_validation3_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['part_validation4_error'])) {
            $errors['part_validation4_error'] = ['Fix the errors before submitting.'];
        }

        if (!empty($this->data['part_validation5_error'])) {
            $errors['part_validation5_error'] = ['Fix the errors before submitting.'];
        }


        if (!empty($errors)) {
            throw ValidationException::withMessages($errors);
        }
    }

    protected function afterCreate(): void
    {
         // Get the value from the hidden field 'plant'
        $plant = $this->form->getState()['plant'] ?? null;
        $line = $this->form->getState()['line'] ?? null;
        $production = $this->form->getState()['production'] ?? null;
        if ($plant) {
            session(['last_selected_plant_id' => $plant]);
        }
        if ($line) {
            session(['last_selected_line' => $line]);
        }
        if ($production) {
            session(['last_selected_production' => $production]);
        }
    }


    protected function getRedirectUrl(): string
    {
        //return $this->getResource()::getUrl('create'); // Stay on Create Page after saving

        return $this->getResource()::getUrl('create', [
            'plant_id' => $this->data['plant_id'] ?? null,
        ]);
    }

}
