<?php

namespace App\Http\Controllers;

use App\Models\Repair;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;

class BusinessRepairController extends Controller
{
    public function index()
    {
        $repairs = Auth::guard('business')->user()->repairs()->latest('date')->get();

        return Inertia::render('Business/Repairs/Index', [
            'repairs' => $repairs,
        ]);
    }

    public function create()
    {
        return Inertia::render('Business/Repairs/Create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'customer_name' => 'required|string|max:255',
            'phone' => 'required|string|max:20',
            'model' => 'required|string|max:255',
            'problem' => 'required|in:repair,unlock,frp,icloud,network,mdm',
            'charge' => 'required|numeric|min:0',
        ]);

        Auth::guard('business')->user()->repairs()->create([
            'customer_name' => $request->customer_name,
            'phone' => $request->phone,
            'model' => $request->model,
            'problem' => $request->problem,
            'charge' => $request->charge,
            'status' => 'pending',
            'date' => today(),
        ]);

        return redirect()->route('business.repairs.index');
    }

    public function update(Request $request, Repair $repair)
    {
        $request->validate([
            'status' => 'required|in:pending,done',
        ]);

        $repair->update(['status' => $request->status]);

        return redirect()->route('business.repairs.index');
    }
}