<?php

namespace App\Livewire\Inventory\Parts;

use App\Models\Part;
use App\Models\Supplier;
use Livewire\Component;
use Livewire\WithFileUploads;

class Edit extends Component
{
    use WithFileUploads;

    public Part $part;
    
    public $part_number = '';
    public $name = '';
    public $description = '';
    public $manufacturer = '';
    public $category = '';
    public $cost_price = '';
    public $sell_price = '';
    public $quantity_on_hand = 0;
    public $minimum_stock_level = 0;
    public $maximum_stock_level = 0;
    public $location = '';
    public $supplier_id = '';
    public $supplier_part_number = '';
    public $lead_time_days = '';
    public $status = 'active';
    public $barcode = '';
    public $weight = '';
    public $dimensions = '';
    public $warranty_period = '';
    public $image;
    public $currentImage = '';

    protected $rules = [
        'part_number' => 'required|string|max:255',
        'name' => 'required|string|max:255',
        'description' => 'nullable|string',
        'manufacturer' => 'nullable|string|max:255',
        'category' => 'nullable|string|max:255',
        'cost_price' => 'required|numeric|min:0',
        'sell_price' => 'required|numeric|min:0',
        'quantity_on_hand' => 'required|integer|min:0',
        'minimum_stock_level' => 'required|integer|min:0',
        'maximum_stock_level' => 'required|integer|min:0',
        'location' => 'nullable|string|max:255',
        'supplier_id' => 'nullable|exists:suppliers,id',
        'supplier_part_number' => 'nullable|string|max:255',
        'lead_time_days' => 'nullable|integer|min:0',
        'status' => 'required|in:active,inactive',
        'barcode' => 'nullable|string|max:255',
        'weight' => 'nullable|numeric|min:0',
        'dimensions' => 'nullable|string|max:255',
        'warranty_period' => 'nullable|integer|min:0',
        'image' => 'nullable|image|max:2048',
    ];

    public function mount()
    {
        $this->part_number = $this->part->part_number;
        $this->name = $this->part->name;
        $this->description = $this->part->description;
        $this->manufacturer = $this->part->manufacturer;
        $this->category = $this->part->category;
        $this->cost_price = $this->part->cost_price;
        $this->sell_price = $this->part->sell_price;
        $this->quantity_on_hand = $this->part->quantity_on_hand;
        $this->minimum_stock_level = $this->part->minimum_stock_level;
        $this->maximum_stock_level = $this->part->maximum_stock_level;
        $this->location = $this->part->location;
        $this->supplier_id = $this->part->supplier_id;
        $this->supplier_part_number = $this->part->supplier_part_number;
        $this->lead_time_days = $this->part->lead_time_days;
        $this->status = $this->part->status;
        $this->barcode = $this->part->barcode;
        $this->weight = $this->part->weight;
        $this->dimensions = $this->part->dimensions;
        $this->warranty_period = $this->part->warranty_period;
        $this->currentImage = $this->part->image;
    }

    public function save()
    {
        $rules = $this->rules;
        $rules['part_number'] = 'required|string|max:255|unique:parts,part_number,' . $this->part->id;
        
        $this->validate($rules);

        $data = [
            'part_number' => $this->part_number,
            'name' => $this->name,
            'description' => $this->description,
            'manufacturer' => $this->manufacturer,
            'category' => $this->category,
            'cost_price' => $this->cost_price,
            'sell_price' => $this->sell_price,
            'quantity_on_hand' => $this->quantity_on_hand,
            'minimum_stock_level' => $this->minimum_stock_level,
            'maximum_stock_level' => $this->maximum_stock_level,
            'location' => $this->location,
            'supplier_id' => $this->supplier_id ?: null,
            'supplier_part_number' => $this->supplier_part_number,
            'lead_time_days' => $this->lead_time_days,
            'status' => $this->status,
            'barcode' => $this->barcode,
            'weight' => $this->weight,
            'dimensions' => $this->dimensions,
            'warranty_period' => $this->warranty_period,
        ];

        // Handle image upload
        if ($this->image) {
            $data['image'] = $this->image->store('parts', 'public');
        }

        $this->part->update($data);

        session()->flash('success', 'Part updated successfully.');
        
        return $this->redirect(route('inventory.parts.index'));
    }

    public function render()
    {
        $suppliers = Supplier::active()->orderBy('name')->get();
        
        return view('livewire.inventory.parts.edit', [
            'suppliers' => $suppliers,
        ]);
    }
}
