<?php

namespace App\Http\Controllers\Admin\Customer;

use App\Http\Controllers\Controller;
use App\Models\Client;
use App\Models\CustomerAdvancePaymentMaster;
use App\Models\CustomerAdvancePaymentDetail;
use App\Service\Customer\CustomerAdvancePaymentService;
use Illuminate\Http\Request;
use Inertia\Inertia;

class CustomerAdvancePaymentController extends Controller
{
    public function index()
    {
        return Inertia::render('Pos/CustomerAdvancePayment');
    }

    public function view()
    {
        $lastRecord = CustomerAdvancePaymentMaster::orderBy('id', 'DESC')->first();
        $reference = invoice_no($lastRecord?->id ?? false, 'CAP');

        $advancePayments = CustomerAdvancePaymentMaster::with('customer', 'warehouse', 'details', 'payments')
            ->orderBy('id', 'DESC')
            ->get();

        return response()->json([
            'reference' => $reference,
            'advancePayments' => $advancePayments
        ]);
    }

    public function store(Request $request)
    {
        try {
            CustomerAdvancePaymentService::store($request->all());

            $lastRecord = CustomerAdvancePaymentMaster::orderBy('id', 'DESC')->first();
            $reference = invoice_no($lastRecord?->id ?? false, 'CAP');

            $advancePayments = CustomerAdvancePaymentMaster::with('customer', 'warehouse', 'details', 'payments')
                ->orderBy('id', 'DESC')
                ->get();

            return response()->json([
                'success' => 'Advance Payment Created Successfully',
                'reference' => $reference,
                'advancePayments' => $advancePayments
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage()
            ], 500);
        }
    }

    public function update(Request $request, $id)
    {
        try {
            CustomerAdvancePaymentService::update($request->all(), $id);

            $lastRecord = CustomerAdvancePaymentMaster::orderBy('id', 'DESC')->first();
            $reference = invoice_no($lastRecord?->id ?? false, 'CAP');

            $advancePayments = CustomerAdvancePaymentMaster::with('customer', 'warehouse', 'details', 'payments')
                ->orderBy('id', 'DESC')
                ->get();

            return response()->json([
                'success' => 'Advance Payment Updated Successfully',
                'reference' => $reference,
                'advancePayments' => $advancePayments
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage()
            ], 500);
        }
    }

    public function destroy($id)
    {
        $master = CustomerAdvancePaymentMaster::findOrFail($id);
        
        // Delete related payments
        \App\Models\CustomerPaymentCollection::where('advance_payment_master_id', $id)->delete();
        
        // Delete details (cascade should handle this, but being explicit)
        CustomerAdvancePaymentDetail::where('advance_payment_master_id', $id)->delete();
        
        // Delete master
        $master->delete();

        $lastRecord = CustomerAdvancePaymentMaster::orderBy('id', 'DESC')->first();
        $reference = invoice_no($lastRecord?->id ?? false, 'CAP');

        $advancePayments = CustomerAdvancePaymentMaster::with('customer', 'warehouse', 'details', 'payments')
            ->orderBy('id', 'DESC')
            ->get();

        return response()->json([
            'success' => 'Advance Payment Deleted Successfully',
            'reference' => $reference,
            'advancePayments' => $advancePayments
        ]);
    }
}
