<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Branch;
use App\Models\Customer;
use App\Models\Product;
use App\Models\ServiceClaim;
use App\Models\Warranty;
use App\Support\AdminAudit;
use Illuminate\Http\Request;
class ServiceController extends Controller
{
    public function index() {
        $claims = ServiceClaim::latest('id')->paginate(20);
        $warranties = Warranty::latest('id')->paginate(20);
        $customers = Customer::orderBy('name')->get();
        $products = Product::orderBy('product_name')->get();
        $branches = Branch::orderBy('name')->get();
        return view('admin.service.index', compact('claims','warranties','customers','products','branches'));
    }
    public function storeWarranty(Request $request) {
        $data = $request->validate([
            'customer_id'=>'nullable|exists:customers,id','sale_invoice_no'=>'nullable|string|max:60','product_id'=>'required|exists:products,id','serial_no'=>'nullable|string|max:100',
            'warranty_period_months'=>'required|integer|min:0','start_date'=>'required|date','status'=>'required|in:Active,Expired,Claimed','notes'=>'nullable|string'
        ]);
        $start = \Carbon\Carbon::parse($data['start_date']);
        $data['end_date'] = $start->copy()->addMonths((int)$data['warranty_period_months'])->toDateString();
        $w = Warranty::create($data);
        AdminAudit::log('Warranty','Create',(string)$w->id,null,$w->toArray());
        return back()->with('success','Warranty card created successfully.');
    }
    public function storeClaim(Request $request) {
        $data = $request->validate([
            'customer_id'=>'nullable|exists:customers,id','warranty_id'=>'nullable|exists:warranties,id','product_id'=>'required|exists:products,id','branch_id'=>'nullable|exists:branches,id',
            'issue_title'=>'required|string|max:150','issue_details'=>'nullable|string','technician_name'=>'nullable|string|max:100','status'=>'required|in:Received,In Repair,Delivered,Replacement,Closed',
            'service_cost'=>'nullable|numeric|min:0','replacement_product_id'=>'nullable|exists:products,id','received_at'=>'nullable|date','delivered_at'=>'nullable|date','notes'=>'nullable|string'
        ]);
        $data['claim_no'] = 'CLM-'.now()->format('YmdHis');
        $data['repair_ticket_no'] = 'RPR-'.now()->format('YmdHis');
        $claim = ServiceClaim::create($data);
        AdminAudit::log('Service Claim','Create',$claim->claim_no,null,$claim->toArray());
        return back()->with('success','Service claim saved successfully.');
    }
    public function warrantyCheck(Request $request) {
        $serial = $request->get('serial_no');
        $invoice = $request->get('invoice');
        $result = Warranty::when($serial, fn($q)=>$q->where('serial_no',$serial))->when($invoice, fn($q)=>$q->orWhere('sale_invoice_no',$invoice))->latest('id')->first();
        return view('admin.service.warranty_check', compact('result','serial','invoice'));
    }
}
