<?php

namespace App\Http\Controllers;

use App\Models\OmsClient;
use App\Models\OmsDealer;
use Illuminate\Http\Request;

class OmsClientController extends Controller
{
    public function index()
    {
        $clients = OmsClient::with('dealer')->latest()->get();
        return view('pages.oms_clients.index', compact('clients'));
    }

    public function create()
    {
        $dealers = OmsDealer::select('id', 'dealer_name')->get();
        return view('pages.oms_clients.create', compact('dealers'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'date' => 'required|date',
            'client_name' => 'required|string|max:255',
            'guardian_name' => 'required|string|max:255',
            'address' => 'nullable|string',
            'nid_number' => 'required|string|max:255',
            'mobile_number' => 'nullable|string|max:20',
            'product_name' => 'nullable|string|max:255',
            'dealer_id' => 'required|exists:oms_dealers,id',
            'word_no' => 'required|string',
            'institute_name' => 'required|string',
            'sale_point' => 'required|string',
        ]);

        OmsClient::create($validated);
        return redirect()->route('oms_clients.index')->with('success', 'Client added successfully.');
    }

    public function edit($id)
    {
        $client = OmsClient::findOrFail($id);
        $dealers = OmsDealer::select('id', 'dealer_name')->get();
        return view('pages.oms_clients.edit', compact('client', 'dealers'));
    }

    public function update(Request $request, $id)
    {
        $client = OmsClient::findOrFail($id);
        $validated = $request->validate([
            'date' => 'required|date',
            'client_name' => 'required|string|max:255',
            'guardian_name' => 'required|string|max:255',
            'address' => 'nullable|string',
            'nid_number' => 'required|string|max:255',
            'mobile_number' => 'nullable|string|max:20',
            'product_name' => 'nullable|string|max:255',
            'dealer_id' => 'required|exists:oms_dealers,id',
            'word_no' => 'required|string',
            'institute_name' => 'required|string',
            'sale_point' => 'required|string',
        ]);

        $client->update($validated);
        return redirect()->route('oms_clients.index')->with('success', 'Client updated successfully.');
    }

    public function destroy($id)
    {
        OmsClient::findOrFail($id)->delete();
        return back()->with('success', 'Client deleted successfully.');
    }

    // AJAX Method for Auto-fill (Fast Response)
    public function getDealerDetails($id)
    {
        $dealer = OmsDealer::select('word_number', 'institute_name', 'sale_point')->find($id);
        return response()->json($dealer);
    }

    public function print($id)
    {
        $client = OmsClient::findOrFail($id);
        return view('pages.oms_clients.print', compact('client'));
    }
}