<?php

namespace Modules\Accounting\Controllers;

use core\Controller;
use core\Auth;
use core\Database;
use core\Security;

class IncomeController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index(): void
    {
        $this->requirePermission('accounting_income_view');

        $db = Database::getInstance();
        $categoryId = $this->request->get('category_id', '');
        $fromDate = $this->request->get('from_date', '');
        $toDate = $this->request->get('to_date', '');
        $page = max(1, (int)$this->request->get('page', 1));
        $perPage = PAGINATION_LIMIT;

        $query = $db->table('income i')
            ->select('i.*, ic.name as category_name')
            ->leftJoin('income_categories ic', 'i.category_id', '=', 'ic.id');

        if (!empty($categoryId)) {
            $query->where('i.category_id', $categoryId);
        }

        if (!empty($fromDate)) {
            $query->where('i.date', '>=', $fromDate);
        }

        if (!empty($toDate)) {
            $query->where('i.date', '<=', $toDate);
        }

        $income = $query->orderBy('i.date', 'DESC')->orderBy('i.created_at', 'DESC')->paginate($perPage, $page);

        $categories = $db->table('income_categories')
            ->orderBy('name', 'ASC')
            ->get();

        $this->view('Accounting/income/index', [
            'title' => 'Income',
            'breadcrumb' => ['Income' => url('income')],
            'income' => $income,
            'categories' => $categories,
            'categoryId' => $categoryId,
            'fromDate' => $fromDate,
            'toDate' => $toDate,
        ]);
    }

    public function create(): void
    {
        $this->requirePermission('accounting_income_create');

        $db = Database::getInstance();
        $categories = $db->table('income_categories')
            ->orderBy('name', 'ASC')
            ->get();

        $this->view('Accounting/income/create', [
            'title' => 'Add Income',
            'breadcrumb' => [
                'Income' => url('income'),
                'Add New' => url('income/create'),
            ],
            'categories' => $categories,
        ]);
    }

    public function store(): void
    {
        $this->requirePermission('accounting_income_create');

        $validated = $this->validate([
            'category_id' => 'required|integer',
            'amount' => 'required|numeric|min:0',
            'description' => 'required|max_length:500',
            'date' => 'required|date',
            'payment_method' => 'required|in:cash,card,bank_transfer,gift_card,advance',
        ]);

        $validated['amount'] = (float)$validated['amount'];
        $validated['created_by'] = Auth::id();

        $incomeId = Database::getInstance()->table('income')->insert($validated);
        $this->logActivity('create', 'Created income: ' . $validated['description']);
        $this->logAudit('create', 'income', $incomeId, null, $validated);

        $this->redirectWith(url('income'), 'success', 'Income recorded successfully.');
    }
}
