<?php

namespace App\Http\Controllers;

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

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

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

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

    public function store(Request $request)
    {
        $request->validate([
            'customer_name' => 'required|string|max:255',
            'phone' => 'required|string|max:20',
            'mobile_model' => 'required|string|max:255',
            'imei' => 'required|string|max:20',
            'buy_price' => 'required|numeric|min:0',
            'condition' => 'required|string|max:255',
        ]);

        Auth::guard('business')->user()->secondHands()->create([
            'customer_name' => $request->customer_name,
            'phone' => $request->phone,
            'mobile_model' => $request->mobile_model,
            'imei' => $request->imei,
            'buy_price' => $request->buy_price,
            'condition' => $request->condition,
            'date' => today(),
        ]);

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