<?php

namespace App\Livewire\Companies;

use App\Models\Company;
use Illuminate\Support\Facades\Storage;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\WithFileUploads;

#[Layout('layouts.app')]
class CompanyForm extends Component
{
    use WithFileUploads;

    public ?Company $company = null;

    // Form fields
    public string $name = '';

    public string $apodo = '';

    public string $tax_id = '';

    public string $estado = 'activo';

    public string $type = 'other';

    public string $address = '';

    public string $phone = '';

    public string $email = '';

    public string $website = '';

    public string $notes = '';

    public $logo = null;

    public function mount(?Company $company = null): void
    {
        if ($company && $company->exists) {
            $this->company = $company;
            $this->name = $company->name;
            $this->apodo = $company->apodo ?? '';
            $this->tax_id = $company->tax_id ?? '';
            $this->estado = $company->estado ?? 'activo';
            $this->type = $company->type ?? 'other';
            $this->address = $company->address ?? '';
            $this->phone = $company->phone ?? '';
            $this->email = $company->email ?? '';
            $this->website = $company->website ?? '';
            $this->notes = $company->notes ?? '';
        }
    }

    protected function rules(): array
    {
        $id = $this->company?->id ?? 'NULL';

        return [
            'name' => 'required|string|max:255',
            'apodo' => 'nullable|string|max:100',
            'tax_id' => "nullable|string|max:50|unique:companies,tax_id,{$id}",
            'estado' => 'required|in:activo,inactivo,suspendido',
            'type' => 'required|in:owner,constructor,subcontractor,consultant,supplier,other',
            'address' => 'nullable|string',
            'phone' => 'nullable|string|max:30',
            'email' => 'nullable|email|max:255',
            'website' => 'nullable|url|max:255',
            'notes' => 'nullable|string',
            'logo' => 'nullable|image|max:2048',
        ];
    }

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

        $data = [
            'name' => $this->name,
            'apodo' => $this->apodo ?: null,
            'tax_id' => $this->tax_id ?: null,
            'estado' => $this->estado,
            'type' => $this->type,
            'address' => $this->address ?: null,
            'phone' => $this->phone ?: null,
            'email' => $this->email ?: null,
            'website' => $this->website ?: null,
            'notes' => $this->notes ?: null,
        ];

        if ($this->logo) {
            // Delete old logo when replacing
            if ($this->company?->logo_path) {
                Storage::disk('public')->delete($this->company->logo_path);
            }
            $data['logo_path'] = $this->logo->store('company-logos', 'public');
        }

        if ($this->company && $this->company->exists) {
            $this->company->update($data);
            session()->flash('notify', 'Empresa actualizada correctamente.');
        } else {
            Company::create($data);
            session()->flash('notify', 'Empresa creada correctamente.');
        }

        $this->redirect(route('companies.manage'), navigate: true);
    }

    public function render()
    {
        return view('livewire.companies.company-form');
    }
}
