<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\CreditDeductable;

use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Validator;

use App\Models\CreditsDeductable;
use App\Models\CommitteeApprovalArchive;
use App\Models\CreditRequest;

class CreditCommitteeApprovalController extends Controller
{
    /**
     * Archive a credit record by moving it from
     * creditcommittee_approvals to archivecreditcommittee_approvals
     */
    public function archivePayedCredit($id)
    {
        try {
            // 1️⃣ Find the record in the main table
            $credit = DB::table('creditcommittee_approvals')->where('id', $id)->first();

            if (!$credit) {
                return response()->json(['message' => 'Credit not found'], 404);
            }

            // 2️⃣ Insert record into the archive table (keeping same ID)
            DB::table('archivecreditcommittee_approvals')->insert([
                'id' => $credit->id,
                'client_id' => $credit->client_id,
                'seed_id' => $credit->seed_id,
                'approved_amount' => $credit->approved_amount,
                'description' => $credit->description,
                'comments' => $credit->comments,
                'service_charges' => $credit->service_charges,
                'mandatory_savings' => $credit->mandatory_savings,
                'total_deductions' => $credit->total_deductions,
                'total_loan_cost' => $credit->total_loan_cost,
                'cash_to_client' => $credit->cash_to_client,
                'total_repayable' => $credit->total_repayable,
                'original_agent_proposal' => $credit->original_agent_proposal,
                'client_original_request' => $credit->client_original_request,
                'payedAmount' => $credit->payedAmount,
                'created_at' => $credit->created_at,
                'updated_at' => $credit->updated_at,
                'createdby' => $credit->createdby,
            ]);

            // 3️⃣ Delete record from original table
            DB::table('creditcommittee_approvals')->where('id', $id)->delete();

            return response()->json([
                'message' => 'Credit archived successfully',
                'archived_credit_id' => $id,
            ], 200);

        } catch (\Exception $e) {
            return response()->json([
                'error' => 'An error occurred while archiving the credit',
                'details' => $e->getMessage()
            ], 500);
        }
    }


public function loanaproved(){
 $records = CommitteeApprovalArchive::with([
        'client',
        'seed:id,seeds_name',
        'creditRequest' => function ($query) {
            $query->select(
                'id',
                'client_id',
                'request_date',
                'requested_amount',
                'purpose',
                'status',
                'disbursement_date',
                'interest_rate',
                'guarantee_type'
            );
        },
        'CreditDeductable' => function ($query) {
            $query->select(
                'id',
                'client_id',
                'request_date',
                'requested_amount',
                'purpose',
                'status',
                'disbursement_date',
                'interest_rate',
                'guarantee_type'
            );
        }
    ])->latest()->get();

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

}
