<?php

namespace App\Http\Controllers;

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

class BusinessUnlockOrderController extends Controller
{
    public function index()
    {
        $orders = Auth::guard('business')->user()->unlockOrders()->latest()->get();

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

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

    public function store(Request $request)
    {
        $request->validate([
            'customer_name' => 'required|string|max:255',
            'phone' => 'required|string|max:20',
            'device_model' => 'required|string|max:255',
            'imei' => 'required|string|max:20',
            'service_type' => 'required|in:frp,icloud,network,mdm',
            'note' => 'nullable|string',
        ]);

        if (!Auth::guard('business')->user()->canCreateOrder()) {
            return back()->withErrors(['general' => 'Plan limit reached or expired.']);
        }

        Auth::guard('business')->user()->unlockOrders()->create($request->all());

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