<?php

namespace App\Http\Livewire;

use App\Enums\OrientationsEnum;
use App\Settings\GeneralSetting;
use Illuminate\Validation\Rules\Enum;
use Spatie\LivewireWizard\Components\StepComponent;

class DirectionStep extends StepComponent
{

    public string|null $orientation = null;
    public string|null $orientation_secondaire = null;
    public bool $double_orientation = false;

    public function mount()
    {
        $this->double_orientation = session()->has('client_input.double_orientation')
            ? session()->get('client_input.double_orientation')
            : false;

        if ($this->double_orientation){
            $this->orientation = app(GeneralSetting::class)->double_orientation_first;
            $this->orientation_secondaire = app(GeneralSetting::class)->double_orientation_second;
        }else{
            $this->orientation = session()->get('client_input.orientation');
            $this->orientation_secondaire = null;
        }
    }

    public function submit()
    {
        $this->validate([
            'orientation' => ["required", new Enum(OrientationsEnum::class)],
            'double_orientation' => ["required", "bool"],
        ]);

        session()->put('client_input.orientation', $this->orientation);
        session()->put('client_input.double_orientation', $this->double_orientation);

        $this->showStep('resume-step');
    }

    public function render()
    {
        return view('livewire.direction-step');
    }
}
