<?php
declare(strict_types=1);

namespace App\Modules\Accounting\Models;

use App\Core\Model;
use App\Core\App;

class JournalEntryLine extends Model
{
    protected static string $table = 'journal_entry_lines';
    protected static array $fillable = [
        'journal_entry_id', 'line_number', 'account_id', 'description_ar',
        'description_en', 'debit', 'credit', 'currency', 'cost_center_id',
        'branch_id', 'member_id', 'employee_id', 'supplier_id',
        'reference_type', 'reference_id', 'notes',
    ];
    protected static bool $timestamps = false;
    protected static bool $softDelete = false;
    protected static bool $autoTrackAuthor = false;

    public function account(): ?array
    {
        return App::getInstance()->db()->selectOne(
            "SELECT * FROM chart_of_accounts WHERE id = ?",
            [$this->account_id]
        );
    }

    public static function getByJournalEntry(int $journalEntryId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT jel.*, coa.account_code, coa.name_ar as account_name_ar, coa.name_en as account_name_en
             FROM journal_entry_lines jel
             JOIN chart_of_accounts coa ON coa.id = jel.account_id
             WHERE jel.journal_entry_id = ?
             ORDER BY jel.line_number ASC",
            [$journalEntryId]
        );
    }
}
