<?php
declare(strict_types=1);

namespace App\Modules\Accounting\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Core\App;
use App\Modules\Accounting\Services\CrossEntitySettlementService;

class SettlementController extends Controller
{
    public function index(Request $request): Response
    {
        $this->authorize('accounting.settlements.view');
        $db = App::getInstance()->db();

        $settlements = $db->select(
            "SELECT * FROM cross_entity_settlements ORDER BY created_at DESC LIMIT 100"
        );

        return $this->view('Accounting.Views.settlements.index', [
            'settlements' => $settlements,
        ]);
    }

    public function create(Request $request): Response
    {
        $this->authorize('accounting.settlements.manage');
        $db = App::getInstance()->db();

        $suppliers = $db->select("SELECT id, name_ar FROM suppliers WHERE is_archived = 0 ORDER BY name_ar");
        $members = $db->select("SELECT id, full_name_ar, membership_number FROM members WHERE is_archived = 0 ORDER BY full_name_ar LIMIT 500");
        $bankAccounts = $db->select("SELECT id, account_name_ar FROM bank_accounts WHERE is_active = 1 ORDER BY account_name_ar");

        return $this->view('Accounting.Views.settlements.form', [
            'suppliers' => $suppliers,
            'members' => $members,
            'bankAccounts' => $bankAccounts,
        ]);
    }

    public function store(Request $request): Response
    {
        $this->authorize('accounting.settlements.manage');

        $data = [
            'settlement_date' => $request->post('settlement_date'),
            'from_entity_type' => $request->post('from_entity_type'),
            'from_entity_id' => $request->post('from_entity_id'),
            'from_entity_name' => $request->post('from_entity_name'),
            'to_entity_type' => $request->post('to_entity_type'),
            'to_entity_id' => $request->post('to_entity_id'),
            'to_entity_name' => $request->post('to_entity_name'),
            'amount' => $request->post('amount'),
            'purpose' => $request->post('purpose', 'payment'),
            'description' => $request->post('description'),
        ];

        $result = CrossEntitySettlementService::create($data);

        if (!$result['success']) {
            return $this->redirect('/accounting/settlements/create')->withError($result['error']);
        }

        return $this->redirect('/accounting/settlements')->withSuccess('تم إنشاء التسوية بنجاح — رقم: ' . $result['settlement_number']);
    }

    public function approve(Request $request, string $id): Response
    {
        $this->authorize('accounting.settlements.approve');

        $result = CrossEntitySettlementService::approve((int) $id);

        if (!$result['success']) {
            return $this->redirect('/accounting/settlements')->withError($result['error']);
        }

        return $this->redirect('/accounting/settlements')->withSuccess('تم اعتماد وترحيل التسوية بنجاح');
    }
}
