<?php
declare(strict_types=1);

return function (\App\Core\Database $db): void {
    $now = date('Y-m-d H:i:s');

    // 1. GL Account for Membership Sub-Treasury
    $glExists = $db->selectOne(
        "SELECT 1 FROM chart_of_accounts WHERE account_code = ?",
        ['12060103']
    );
    if (!$glExists) {
        $db->insert('chart_of_accounts', [
            'account_code'    => '12060103',
            'name_ar'         => 'صندوق الخزنة الفرعية - العضويات',
            'name_en'         => 'Sub-Treasury Cash - Memberships',
            'parent_code'     => '120601',
            'level'           => 5,
            'account_nature'  => 'debit',
            'is_header'       => 0,
            'is_active'       => 1,
            'current_balance' => '0.00',
            'created_at'      => $now,
            'updated_at'      => $now,
        ]);
    }

    // 2. Treasury record for SUB_MEM
    $treasuryExists = $db->selectOne("SELECT 1 FROM treasuries WHERE code = ?", ['SUB_MEM']);
    if (!$treasuryExists) {
        $db->insert('treasuries', [
            'code'         => 'SUB_MEM',
            'name_ar'      => 'خزنة العضويات الفرعية',
            'name_en'      => 'Memberships Sub-Treasury',
            'type'         => 'sub',
            'account_code' => '12060103',
            'branch_id'    => null,
            'is_active'    => 1,
            'created_at'   => $now,
            'updated_at'   => $now,
        ]);
    }
};
