<?php

namespace App\Http\Livewire\Tenant;

use App\Models\Service;
use App\Models\ServiceCategory;
use Illuminate\Support\Str;
use Livewire\Attributes\Validate;
use Livewire\Component;

class ServiceForm extends Component
{
    public ?Service $service = null;

    #[Validate('required|string|max:255')]
    public string $name = '';

    #[Validate('nullable|string|max:2000')]
    public string $description = '';

    #[Validate('nullable|exists:service_categories,id')]
    public ?int $service_category_id = null;

    #[Validate('required|numeric|min:0')]
    public float $price = 0;

    #[Validate('required|integer|min:5')]
    public int $duration_minutes = 30;

    #[Validate('integer|min:0')]
    public int $buffer_minutes = 0;

    public bool $is_active = true;
    public bool $is_public = true;
    public bool $is_home_service = false;

    public function mount($service = null, ?int $serviceId = null): void
    {
        if ($service instanceof Service || $serviceId) {
            $this->service = $service instanceof Service ? $service : Service::findOrFail($serviceId);
            $data = $this->service->only([
                'name', 'description', 'service_category_id', 'price',
                'duration_minutes', 'buffer_minutes',
                'is_active', 'is_public', 'is_home_service',
            ]);

            $data['description'] = $data['description'] ?? '';
            $data['price'] = (float) $data['price'];
            $data['duration_minutes'] = (int) $data['duration_minutes'];
            $data['buffer_minutes'] = (int) $data['buffer_minutes'];
            $data['is_active'] = (bool) $data['is_active'];
            $data['is_public'] = (bool) $data['is_public'];
            $data['is_home_service'] = (bool) $data['is_home_service'];

            $this->fill($data);
        }
    }

    public function save()
    {
        $this->validate();

        $data = [
            'name'                => $this->name,
            'description'         => $this->description ?: null,
            'service_category_id' => $this->service_category_id,
            'price'               => $this->price,
            'duration_minutes'    => $this->duration_minutes,
            'buffer_minutes'      => $this->buffer_minutes,
            'is_active'           => $this->is_active,
            'is_public'           => $this->is_public,
            'is_home_service'     => $this->is_home_service,
        ];

        if ($this->service) {
            $this->service->update($data);
            $this->service->refresh();
        } else {
            $data['slug'] = Str::slug($this->name).'-'.Str::random(4);
            $this->service = Service::create($data);
        }

        session()->flash('success', $this->service?->wasRecentlyCreated ? 'Servicio creado correctamente.' : 'Servicio actualizado correctamente.');
        return redirect()->route('tenant.services.index');
    }

    public function render()
    {
        return view('livewire.tenant.service-form', [
            'categories' => ServiceCategory::active()->ordered()->get(),
        ]);
    }
}
