<?php

namespace App\Http\Controllers\Admin\Setting;

use App\Http\Controllers\Controller;
use App\Models\Brand;
use App\Models\ProductQuotationSetting;
use App\Models\QuotationType;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ProductCotationSettingController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $QuotationList = ProductQuotationSetting::with('quotationType')->orderbyDesc('id')->get();

        return Inertia::render('Quotation/ProductCotationSetting', compact('QuotationList'));
    }

    public function view()
    {
        return response()->json(['brands' => Brand::orderByDesc('id')->get()]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'header' => 'required|string',
            'footer' => 'required|string',
            'quotation_type_id' => 'nullable|exists:quotation_types,id',
        ]);

        ProductQuotationSetting::create([
            'title' => $request->title,
            'header' => $request->header,
            'footer' => $request->footer,
            'quotation_type_id' => $request->quotation_type_id,
        ]);

        return response()->json([
            'success' => 'Product Quotation created successfully',
            'quotation' => ProductQuotationSetting::with('quotationType')->orderByDesc('id')->get()
        ]);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id) {}

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'header' => 'required|string',
            'footer' => 'required|string',
            'quotation_type_id' => 'nullable|exists:quotation_types,id',
        ]);

        $quotation = ProductQuotationSetting::findOrFail($id);
        $quotation->update([
            'title' => $request->title,
            'header' => $request->header,
            'footer' => $request->footer,
            'quotation_type_id' => $request->quotation_type_id,
        ]);

        return response()->json([
            'success' => 'Product Quotation updated successfully',
            'quotation' => ProductQuotationSetting::with('quotationType')->orderByDesc('id')->get()
        ]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        ProductQuotationSetting::destroy($id);

        return response()->json(['success' => 'Product Quotation delete Successfully', 'quotation' => ProductQuotationSetting::orderByDesc('id')->get()]);
    }

    public function search()
    {

        $brand = Brand::where('name', 'like', '%'.request()->data.'%')->limit(10)->get();

        return response()->json($brand);
    }
}
