<?php

namespace App\Livewire\ServiceItems;

use App\Models\ServiceItem;
use Livewire\Component;
use Livewire\WithPagination;

class Manage extends Component
{
    use WithPagination;

    public $service_name = '';
    public $description = '';
    public $category = '';
    public $labor_rate = 85.00;
    public $estimated_hours = 1.0;
    public $status = 'active';
    public $technician_notes = '';
    
    public $editingId = null;
    public $showForm = false;
    public $searchTerm = '';
    public $categoryFilter = '';
    
    public $categories = [
        'Diagnosis' => 'Diagnosis',
        'Engine' => 'Engine',
        'Brake' => 'Brake',
        'Suspension' => 'Suspension',
        'Electrical' => 'Electrical',
        'Transmission' => 'Transmission',
        'Maintenance' => 'Maintenance',
        'HVAC' => 'HVAC',
        'Cooling' => 'Cooling',
        'Exterior' => 'Exterior',
        'Interior' => 'Interior',
        'Other' => 'Other',
    ];

    protected $rules = [
        'service_name' => 'required|string|max:255',
        'description' => 'nullable|string',
        'category' => 'required|string|max:100',
        'labor_rate' => 'required|numeric|min:0|max:500',
        'estimated_hours' => 'required|numeric|min:0.25|max:40',
        'status' => 'required|in:active,inactive',
        'technician_notes' => 'nullable|string',
    ];

    protected $messages = [
        'service_name.required' => 'Service name is required.',
        'category.required' => 'Category is required.',
        'labor_rate.required' => 'Labor rate is required.',
        'labor_rate.min' => 'Labor rate must be at least $0.',
        'labor_rate.max' => 'Labor rate cannot exceed $500/hour.',
        'estimated_hours.required' => 'Estimated hours is required.',
        'estimated_hours.min' => 'Estimated hours must be at least 0.25 hours.',
        'estimated_hours.max' => 'Estimated hours cannot exceed 40 hours.',
    ];

    public function updatingSearchTerm()
    {
        $this->resetPage();
    }

    public function updatingCategoryFilter()
    {
        $this->resetPage();
    }

    public function toggleForm()
    {
        $this->showForm = !$this->showForm;
        if (!$this->showForm) {
            $this->resetForm();
        }
    }

    public function resetForm()
    {
        $this->reset([
            'service_name', 'description', 'category', 'labor_rate', 
            'estimated_hours', 'status', 'technician_notes', 'editingId'
        ]);
        $this->labor_rate = 85.00;
        $this->estimated_hours = 1.0;
        $this->status = 'active';
    }

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

        if ($this->editingId) {
            // Update existing service item
            ServiceItem::findOrFail($this->editingId)->update([
                'service_name' => $this->service_name,
                'description' => $this->description,
                'category' => $this->category,
                'labor_rate' => $this->labor_rate,
                'estimated_hours' => $this->estimated_hours,
                'status' => $this->status,
                'technician_notes' => $this->technician_notes,
            ]);
            
            session()->flash('message', 'Service item updated successfully!');
        } else {
            // Create new service item
            ServiceItem::create([
                'service_name' => $this->service_name,
                'description' => $this->description,
                'category' => $this->category,
                'labor_rate' => $this->labor_rate,
                'estimated_hours' => $this->estimated_hours,
                'status' => $this->status,
                'technician_notes' => $this->technician_notes,
            ]);
            
            session()->flash('message', 'Service item created successfully!');
        }

        $this->resetForm();
        $this->showForm = false;
    }

    public function edit($id)
    {
        $serviceItem = ServiceItem::findOrFail($id);
        
        $this->editingId = $id;
        $this->service_name = $serviceItem->service_name;
        $this->description = $serviceItem->description;
        $this->category = $serviceItem->category;
        $this->labor_rate = $serviceItem->labor_rate;
        $this->estimated_hours = $serviceItem->estimated_hours;
        $this->status = $serviceItem->status;
        $this->technician_notes = $serviceItem->technician_notes;
        $this->showForm = true;
    }

    public function delete($id)
    {
        ServiceItem::findOrFail($id)->delete();
        session()->flash('message', 'Service item deleted successfully!');
    }

    public function render()
    {
        $query = ServiceItem::query();

        if ($this->searchTerm) {
            $query->where(function ($q) {
                $q->where('service_name', 'like', '%' . $this->searchTerm . '%')
                  ->orWhere('description', 'like', '%' . $this->searchTerm . '%');
            });
        }

        if ($this->categoryFilter) {
            $query->where('category', $this->categoryFilter);
        }

        $serviceItems = $query->orderBy('service_name')->paginate(15);

        return view('livewire.service-items.manage', [
            'serviceItems' => $serviceItems
        ]);
    }
}
