<?php

namespace App\Http\Controllers;

use App\Models\GeneralLedger;
use App\Models\Currency;
use App\Models\SupplierAccount;
use App\Models\SupplierJournalTemp;
use App\Models\SupplierJournal;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SupplierJournalController extends Controller
{
    public function index()
    {
        // Get supplier control account
        $supplierAccount = GeneralLedger::where('account_name', 'like', '%supplier%')->first();
        
        $currencies = Currency::all();
        $suppliers = SupplierAccount::orderBy('supplier_account')->get();
        $tempEntries = SupplierJournalTemp::all(); // Show all entries (Pending + Processed) if needed

        return view('supplierJournal.supplier-journal', compact(
            'supplierAccount',
            'currencies',
            'suppliers',
            'tempEntries'
        ));
    }

    public function store(Request $request)
    {
        $request->validate([
            'date' => 'required|date',
            'supplier_account_id' => 'required|exists:supplier_accounts,id',
            'narration' => 'required|string|max:255',
            'reference' => 'required|string|max:100',
            'currency_id' => 'required|exists:currencies,id',
            'tr_code' => 'required|in:DR,CR',
            'amount' => 'required|numeric|min:0'
        ]);

        SupplierJournalTemp::create([
            'date' => $request->date,
            'supplier_account_id' => $request->supplier_account_id,
            'narration' => $request->narration,
            'reference' => $request->reference,
            'currency_id' => $request->currency_id,
            'tr_code' => $request->tr_code,
            'amount' => $request->amount,
            'status' => 'Pending'
        ]);

        return redirect()->route('supplier.journal.index')
            ->with('success', 'Journal entry saved successfully!');
    }

    public function update(Request $request)
    {
        $request->validate([
            'id' => 'required|exists:supplier_journal_temps,id',
            'date' => 'required|date',
            'supplier_account_id' => 'required|exists:supplier_accounts,id',
            'narration' => 'required|string|max:255',
            'reference' => 'required|string|max:100',
            'currency_id' => 'required|exists:currencies,id',
            'tr_code' => 'required|in:DR,CR',
            'amount' => 'required|numeric|min:0'
        ]);

        $entry = SupplierJournalTemp::findOrFail($request->id);

        $entry->update([
            'date' => $request->date,
            'supplier_account_id' => $request->supplier_account_id,
            'narration' => $request->narration,
            'reference' => $request->reference,
            'currency_id' => $request->currency_id,
            'tr_code' => $request->tr_code,
            'amount' => $request->amount,
        ]);

        return redirect()->route('supplier.journal.index')
            ->with('success', 'Journal entry updated successfully!');
    }

    public function destroy($id)
    {
        $entry = SupplierJournalTemp::findOrFail($id);
        $entry->delete();

        return redirect()->route('supplier.journal.index')
            ->with('success', 'Entry deleted successfully!');
    }

    // Process all pending entries
    public function process()
    {
        DB::transaction(function () {
            $tempEntries = SupplierJournalTemp::where('status', 'Pending')->get();

            foreach ($tempEntries as $entry) {
                SupplierJournal::create([
                    'date' => $entry->date,
                    'supplier_account_id' => $entry->supplier_account_id,
                    'narration' => $entry->narration,
                    'reference' => $entry->reference,
                    'currency_id' => $entry->currency_id,
                    'tr_code' => $entry->tr_code,
                    'amount' => $entry->amount
                ]);

                $entry->delete();
            }
        });

        return redirect()->route('supplier.journal.index')
            ->with('success', 'All pending journal entries processed successfully!');
    }

    // Process a single entry
    public function processSingle($id)
    {
        DB::transaction(function () use ($id) {
            $entry = SupplierJournalTemp::findOrFail($id);

            SupplierJournal::create([
                'date' => $entry->date,
                'supplier_account_id' => $entry->supplier_account_id,
                'narration' => $entry->narration,
                'reference' => $entry->reference,
                'currency_id' => $entry->currency_id,
                'tr_code' => $entry->tr_code,
                'amount' => $entry->amount
            ]);

            $entry->delete();
        });

        return redirect()->route('supplier.journal.index')
            ->with('success', 'Single journal entry processed successfully!');
    }
}
