-- =====================================================================
--  FinLend — reference / seed data
--  Run AFTER schema.sql:  php src/Console/migrate.php --fresh --seed
--  Safe to re-run: every insert is INSERT IGNORE or ON DUPLICATE KEY.
-- =====================================================================


-- ---------------------------------------------------------------------
-- Organisation & branches
-- ---------------------------------------------------------------------
INSERT IGNORE INTO organisations
    (id, code, legal_name, trade_name, entity_type, fy_start_month, base_currency, is_active)
VALUES
    (1, 'DEMO', 'Example Finance Private Limited', 'Example Finance', 'nbfc', 4, 'INR', 1);

INSERT IGNORE INTO branches (id, org_id, code, name, branch_type, city, state_code, opening_date, cash_limit)
VALUES
    (1, 1, 'HO',  'Head Office',        'head_office', 'Coimbatore', '33', '2020-04-01', 50000000),
    (2, 1, 'CBE', 'Coimbatore Main',    'branch',      'Coimbatore', '33', '2020-04-01', 20000000),
    (3, 1, 'ERD', 'Erode',              'branch',      'Erode',      '33', '2021-06-01', 15000000);

-- ---------------------------------------------------------------------
-- Permissions — module.action. Grouped so the roles below read clearly.
-- ---------------------------------------------------------------------
INSERT IGNORE INTO permissions (slug, module, description, is_sensitive) VALUES
    ('member.view',            'member',     'View members and their KYC', 0),
    ('member.create',          'member',     'Onboard a new member', 0),
    ('member.update',          'member',     'Edit member details', 0),
    ('member.kyc',             'member',     'Run Aadhaar / PAN / bank verification', 1),
    ('member.kyc_approve',     'member',     'Approve a KYC file', 1),
    ('member.aml',             'member',     'Manage AML screening and blacklist', 1),
    ('group.view',             'group',      'View JLG / SHG groups and centres', 0),
    ('group.create',           'group',      'Form a group', 0),
    ('group.grade',            'group',      'Grade a group', 0),
    ('share.view',             'share',      'View the Nidhi share register', 0),
    ('share.create',           'share',      'Allot shares', 1),
    ('share.transfer',         'share',      'Transfer or surrender shares', 1),
    ('loan.view',              'loan',       'View applications and loan accounts', 0),
    ('loan.create',            'loan',       'Create a loan application', 0),
    ('loan.underwrite',        'loan',       'Run credit assessment', 0),
    ('loan.fi',                'loan',       'Record field verification', 0),
    ('loan.recommend',         'loan',       'Recommend a file for sanction', 0),
    ('loan.approve',           'loan',       'Sanction or reject a loan', 1),
    ('loan.disburse',          'loan',       'Disburse a sanctioned loan', 1),
    ('loan.restructure',       'loan',       'Restructure or reschedule a loan', 1),
    ('loan.close',             'loan',       'Foreclose or close a loan', 1),
    ('loan.writeoff',          'loan',       'Write off a loan', 1),
    ('collateral.view',        'collateral', 'View the collateral register', 0),
    ('collateral.release',     'collateral', 'Release pledged collateral', 1),
    ('bureau.view',            'bureau',     'View bureau enquiry history', 0),
    ('bureau.pull',            'bureau',     'Pull a fresh bureau report', 1),
    ('credit.configure',       'credit',     'Configure scorecards', 1),
    ('credit.bsa',             'credit',     'Run the bank statement analyser', 0),
    ('repayment.view',         'repayment',  'View receipts and collections', 0),
    ('repayment.create',       'repayment',  'Post a repayment', 0),
    ('repayment.reverse',      'repayment',  'Reverse a posted receipt', 1),
    ('repayment.backdate',     'repayment',  'Post with a value date over 30 days old', 1),
    ('repayment.post_written_off', 'repayment', 'Post a recovery against a written-off loan', 1),
    ('repayment.waive',        'repayment',  'Waive interest or penal charges', 1),
    ('cash.view',              'cash',       'View cash position', 0),
    ('cash.remit',             'cash',       'Remit collected cash', 0),
    ('cash.transfer',          'cash',       'Inter-branch cash transfer', 1),
    ('nach.view',              'nach',       'View mandates and batches', 0),
    ('nach.create',            'nach',       'Register a mandate', 0),
    ('nach.present',           'nach',       'Generate and upload a presentation file', 1),
    ('deposit.view',           'deposit',    'View deposit accounts', 0),
    ('deposit.create',         'deposit',    'Open a deposit account', 0),
    ('deposit.interest',       'deposit',    'Run the deposit interest payout', 1),
    ('deposit.lien',           'deposit',    'Mark or release a lien', 1),
    ('accounting.view',        'accounting', 'View the books', 0),
    ('accounting.post',        'accounting', 'Pass a journal entry', 1),
    ('accounting.reconcile',   'accounting', 'Reconcile the bank account', 0),
    ('eod.view',               'eod',        'View day-end history', 0),
    ('eod.run',                'eod',        'Run day-end', 1),
    ('eod.month_close',        'eod',        'Close a month', 1),
    ('eod.year_close',         'eod',        'Close a financial year', 1),
    ('npa.view',               'npa',        'View the SMA / NPA register', 0),
    ('legal.view',             'legal',      'View legal and recovery cases', 0),
    ('report.view',            'report',     'View and export reports', 0),
    ('compliance.view',        'compliance', 'View statutory returns', 0),
    ('compliance.submit',      'compliance', 'Generate and submit a return', 1),
    ('compliance.aml',         'compliance', 'File FIU-IND CTR / STR', 1),
    ('audit.view',             'audit',      'View the audit trail', 0),
    ('audit.verify',           'audit',      'Verify audit chain integrity', 0),
    ('admin.configure',        'admin',      'Change system configuration', 1),
    ('admin.branches',         'admin',      'Manage branches', 1),
    ('admin.products',         'admin',      'Manage loan and deposit products', 1),
    ('admin.users',            'admin',      'Manage users', 1),
    ('admin.roles',            'admin',      'Manage roles and permissions', 1),
    ('admin.integrations',     'admin',      'Manage third-party integrations', 1);

-- ---------------------------------------------------------------------
-- Roles. `scope` drives ROW-LEVEL access, separately from the verb
-- permissions above: a branch manager holds loan.approve but only ever
-- sees their own branch's rows.
-- max_approval_amount is in PAISE.
-- ---------------------------------------------------------------------
INSERT IGNORE INTO roles (id, org_id, slug, name, description, scope, is_system, requires_2fa, max_approval_amount) VALUES
    (1, 1, 'super_admin',    'Super Administrator', 'Break-glass account. 2FA enforced.',        'global', 1, 1, 0),
    (2, 1, 'admin',          'Administrator',       'Head-office administration',               'global', 1, 1, 500000000),
    (3, 1, 'branch_manager', 'Branch Manager',      'Runs one branch; sanctions within limit',  'branch', 1, 0, 50000000),
    (4, 1, 'loan_officer',   'Loan Officer',        'Sources and services loans',               'branch', 1, 0, 0),
    (5, 1, 'collector',      'Collection Officer',  'Field collections only',                   'branch', 1, 0, 0),
    (6, 1, 'accountant',     'Accountant',          'Books, reconciliation and day-end',        'global', 1, 1, 0),
    (7, 1, 'auditor',        'Internal Auditor',    'Read-only across the organisation',        'global', 1, 0, 0),
    (8, 1, 'customer',       'Customer',            'Borrower self-service portal',             'self',   1, 0, 0);

-- Super admin: implicit everything (Rbac::can short-circuits), so no rows.

-- Administrator — everything except the break-glass-only actions.
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 2, id FROM permissions WHERE slug NOT IN ('eod.year_close');

-- Branch manager
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 3, id FROM permissions WHERE slug IN (
    'member.view','member.create','member.update','member.kyc','member.kyc_approve',
    'group.view','group.create','group.grade',
    'loan.view','loan.create','loan.underwrite','loan.fi','loan.recommend','loan.approve','loan.disburse','loan.close',
    'collateral.view','collateral.release',
    'bureau.view','bureau.pull','credit.bsa',
    'repayment.view','repayment.create','repayment.reverse','repayment.waive',
    'cash.view','cash.remit','cash.transfer',
    'nach.view','nach.create','nach.present',
    'deposit.view','deposit.create',
    'accounting.view','accounting.reconcile',
    'eod.view','eod.run','npa.view','legal.view','report.view','compliance.view','audit.view'
);

-- Loan officer — can source and recommend, but NOT sanction. Separation
-- of maker and checker is the whole point of this role.
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 4, id FROM permissions WHERE slug IN (
    'member.view','member.create','member.update','member.kyc',
    'group.view','group.create',
    'loan.view','loan.create','loan.underwrite','loan.fi','loan.recommend',
    'collateral.view','bureau.view','bureau.pull','credit.bsa',
    'repayment.view','repayment.create',
    'cash.view','cash.remit','nach.view','nach.create',
    'deposit.view','npa.view','report.view'
);

-- Collector — deliberately minimal. A field collector can take money and
-- see who owes it; nothing else.
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 5, id FROM permissions WHERE slug IN (
    'member.view','group.view','loan.view',
    'repayment.view','repayment.create',
    'cash.view','cash.remit'
);

-- Accountant
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 6, id FROM permissions WHERE slug IN (
    'accounting.view','accounting.post','accounting.reconcile',
    'eod.view','eod.run','eod.month_close',
    'cash.view','cash.transfer','deposit.view','deposit.interest',
    'loan.view','repayment.view','npa.view','report.view',
    'compliance.view','compliance.submit','audit.view'
);

-- Auditor — read-only, but including the audit tooling itself.
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 7, id FROM permissions WHERE slug LIKE '%.view' OR slug = 'audit.verify';

-- Customer portal
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 8, id FROM permissions WHERE slug IN ('loan.view','repayment.view','deposit.view');

-- ---------------------------------------------------------------------
-- Chart of accounts
-- ---------------------------------------------------------------------
INSERT IGNORE INTO acc_chart_of_accounts
    (id, org_id, code, name, parent_id, account_type, account_subtype, normal_balance, is_group, is_cash_account, is_bank_account, schedule3_head, is_system)
VALUES
    -- Assets
    (1,  1, '1000', 'ASSETS',                        NULL, 'asset',  NULL,                 'debit',  1, 0, 0, NULL, 1),
    (2,  1, '1100', 'Cash and cash equivalents',     1,    'asset',  NULL,                 'debit',  1, 0, 0, 'Cash and cash equivalents', 1),
    (3,  1, '1101', 'Cash in hand',                  2,    'asset',  'cash',               'debit',  0, 1, 0, 'Cash and cash equivalents', 1),
    (4,  1, '1102', 'Cash with collection officers', 2,    'asset',  'cash',               'debit',  0, 1, 0, 'Cash and cash equivalents', 1),
    (5,  1, '1200', 'Bank balances',                 1,    'asset',  NULL,                 'debit',  1, 0, 0, 'Bank balances', 1),
    (6,  1, '1201', 'Bank — current account',        5,    'asset',  'bank',               'debit',  0, 0, 1, 'Bank balances', 1),
    (7,  1, '1300', 'Loans and advances',            1,    'asset',  NULL,                 'debit',  1, 0, 0, 'Loans', 1),
    (8,  1, '1301', 'Loans — principal outstanding', 7,    'asset',  'loan_asset',         'debit',  0, 0, 0, 'Loans', 1),
    (9,  1, '1302', 'Interest receivable',           7,    'asset',  'interest_receivable','debit',  0, 0, 0, 'Other financial assets', 1),
    (10, 1, '1303', 'Penal charges receivable',      7,    'asset',  'other_receivable',   'debit',  0, 0, 0, 'Other financial assets', 1),
    (11, 1, '1304', 'Fees receivable',               7,    'asset',  'other_receivable',   'debit',  0, 0, 0, 'Other financial assets', 1),
    (12, 1, '1305', 'NACH presentations in transit', 7,    'asset',  'other_receivable',   'debit',  0, 0, 0, 'Other financial assets', 1),
    (13, 1, '1401', 'Gold ornaments in custody',     1,    'asset',  'memo',               'debit',  0, 0, 0, 'Contingent', 1),

    -- Liabilities
    (20, 1, '2000', 'LIABILITIES',                   NULL, 'liability', NULL,              'credit', 1, 0, 0, NULL, 1),
    (21, 1, '2100', 'Member deposits',               20,   'liability', NULL,              'credit', 1, 0, 0, 'Deposits', 1),
    (22, 1, '2101', 'Fixed deposits',                21,   'liability', 'deposit_liability','credit', 0, 0, 0, 'Deposits', 1),
    (23, 1, '2102', 'Recurring deposits',            21,   'liability', 'deposit_liability','credit', 0, 0, 0, 'Deposits', 1),
    (24, 1, '2103', 'Savings deposits',              21,   'liability', 'deposit_liability','credit', 0, 0, 0, 'Deposits', 1),
    (25, 1, '2201', 'Advance from borrowers',        20,   'liability', 'advance',         'credit', 0, 0, 0, 'Other financial liabilities', 1),
    (26, 1, '2202', 'Insurance premium payable',     20,   'liability', 'payable',         'credit', 0, 0, 0, 'Other financial liabilities', 1),
    (27, 1, '2203', 'GST payable',                   20,   'liability', 'statutory',       'credit', 0, 0, 0, 'Other liabilities', 1),
    (28, 1, '2204', 'TDS payable',                   20,   'liability', 'statutory',       'credit', 0, 0, 0, 'Other liabilities', 1),
    (29, 1, '2298', 'Interest suspense (NPA / penal)', 20, 'liability', 'suspense',        'credit', 0, 0, 0, 'Other liabilities', 1),
    (30, 1, '2299', 'Suspense — unidentified credits', 20, 'liability', 'suspense',        'credit', 0, 0, 0, 'Other liabilities', 1),
    (31, 1, '2401', 'Provision against loan assets', 20,   'liability', 'provision',       'credit', 0, 0, 0, 'Provisions', 1),

    -- Equity
    (40, 1, '3000', 'EQUITY',                        NULL, 'equity', NULL,                 'credit', 1, 0, 0, NULL, 1),
    (41, 1, '3101', 'Share capital',                 40,   'equity', 'share_capital',      'credit', 0, 0, 0, 'Equity share capital', 1),
    (42, 1, '3201', 'Reserves and surplus',          40,   'equity', 'reserves',           'credit', 0, 0, 0, 'Other equity', 1),
    (43, 1, '3202', 'Statutory reserve (s.45-IC)',   40,   'equity', 'reserves',           'credit', 0, 0, 0, 'Other equity', 1),

    -- Income
    (50, 1, '4000', 'INCOME',                        NULL, 'income', NULL,                 'credit', 1, 0, 0, NULL, 1),
    (51, 1, '4101', 'Interest income on loans',      50,   'income', 'interest_income',    'credit', 0, 0, 0, 'Interest income', 1),
    (52, 1, '4102', 'Penal charges recovered',       50,   'income', 'fee_income',         'credit', 0, 0, 0, 'Fees and commission income', 1),
    (53, 1, '4103', 'Processing fee income',         50,   'income', 'fee_income',         'credit', 0, 0, 0, 'Fees and commission income', 1),
    (54, 1, '4104', 'Documentation charges',         50,   'income', 'fee_income',         'credit', 0, 0, 0, 'Fees and commission income', 1),
    (55, 1, '4105', 'Dishonour charges',             50,   'income', 'fee_income',         'credit', 0, 0, 0, 'Fees and commission income', 1),
    (56, 1, '4106', 'Foreclosure charges',           50,   'income', 'fee_income',         'credit', 0, 0, 0, 'Fees and commission income', 1),
    (57, 1, '4107', 'Recovery of written-off loans', 50,   'income', 'other_income',       'credit', 0, 0, 0, 'Other income', 1),

    -- Expenses
    (60, 1, '5000', 'EXPENSES',                      NULL, 'expense', NULL,                'debit',  1, 0, 0, NULL, 1),
    (61, 1, '5101', 'Interest on member deposits',   60,   'expense', 'finance_cost',      'debit',  0, 0, 0, 'Finance costs', 1),
    (62, 1, '5201', 'Provision for loan losses',     60,   'expense', 'provision_expense', 'debit',  0, 0, 0, 'Impairment on financial instruments', 1),
    (63, 1, '5202', 'Bad debts written off',         60,   'expense', 'provision_expense', 'debit',  0, 0, 0, 'Impairment on financial instruments', 1),
    (64, 1, '5301', 'Credit bureau charges',         60,   'expense', 'operating',         'debit',  0, 0, 0, 'Other expenses', 1),
    (65, 1, '5302', 'SMS and communication charges', 60,   'expense', 'operating',         'debit',  0, 0, 0, 'Other expenses', 1);

UPDATE branches SET cash_gl_id = 3, bank_gl_id = 6 WHERE org_id = 1;

-- ---------------------------------------------------------------------
-- Loan products — one per regulatory class, showing the rule-pack in use
-- Amounts in PAISE, rates in BASIS POINTS.
-- ---------------------------------------------------------------------
INSERT IGNORE INTO loan_products
    (id, org_id, code, name, regulatory_class, loan_category, interest_method, interest_rate_bps,
     min_principal, max_principal, min_tenure, max_tenure, repayment_frequency, grace_days,
     processing_fee_bps, penal_charge_mode, penal_charge_value, penal_charge_cap, foreclosure_charge_bps,
     collateral_required, collateral_type, max_ltv_bps, bureau_check_mandatory, min_cibil_score,
     max_household_income, max_flioi_bps, requires_nidhi_member, min_shares_required,
     gl_principal_id, gl_interest_income_id, gl_interest_receivable_id, gl_fee_income_id, gl_penal_income_id, gl_provision_id,
     approval_levels, nach_mandatory, effective_from, is_active)
VALUES
    -- Microfinance JLG: weekly, no collateral, RBI 2022 income + FOIR caps,
    -- no foreclosure charge (prohibited for microfinance).
    (1, 1, 'MFJLG', 'Microfinance JLG (weekly)', 'mfi_jlg', 'group', 'reducing_balance', 2400,
     1000000, 8000000, 25, 104, 'weekly', 0,
     100, 'percent_per_annum', 1200, 50000, 0,
     0, 'none', NULL, 1, NULL,
     30000000, 5000, 0, 0,
     8, 51, 9, 53, 52, 31,
     2, 0, '2026-04-01', 1),

    -- Nidhi gold loan: bullet, 75% LTV cap, members only.
    (2, 1, 'NDGLD', 'Nidhi Gold Loan', 'nidhi_gold', 'individual', 'bullet', 1400,
     500000, 100000000, 3, 24, 'monthly', 7,
     50, 'percent_per_annum', 2400, 0, 200,
     1, 'gold', 7500, 0, NULL,
     NULL, NULL, 1, 10,
     8, 51, 9, 53, 52, 31,
     1, 0, '2026-04-01', 1),

    -- Nidhi term loan against property.
    (3, 1, 'NDTRM', 'Nidhi Term Loan (property)', 'nidhi_property', 'individual', 'reducing_balance', 1800,
     10000000, 2500000000, 12, 120, 'monthly', 5,
     150, 'percent_per_annum', 2400, 0, 200,
     1, 'property', 6000, 1, 650,
     NULL, 5000, 1, 10,
     8, 51, 9, 53, 52, 31,
     2, 1, '2026-04-01', 1),

    -- NBFC business loan, unsecured.
    (4, 1, 'BIZUN', 'Business Loan (unsecured)', 'nbfc_business', 'individual', 'reducing_balance', 2100,
     5000000, 1000000000, 12, 60, 'monthly', 3,
     200, 'percent_per_annum', 2400, 0, 400,
     0, 'none', NULL, 1, 700,
     NULL, 5500, 0, 0,
     8, 51, 9, 53, 52, 31,
     2, 1, '2026-04-01', 1);

-- ---------------------------------------------------------------------
-- Deposit products (Nidhi). Tenure bounds follow Nidhi Rules 2014 r.13.
-- ---------------------------------------------------------------------
INSERT IGNORE INTO deposit_products
    (id, org_id, code, name, deposit_type, interest_rate_bps, senior_citizen_bonus_bps, compounding, payout_frequency,
     min_amount, min_tenure_months, max_tenure_months, premature_allowed, premature_penalty_bps, premature_lock_months,
     gl_liability_id, gl_interest_expense_id, is_active, effective_from)
VALUES
    (1, 1, 'FD12', 'Fixed Deposit',      'fixed',     800, 50,  'quarterly',   'on_maturity', 500000,  6, 60, 1, 100, 3, 22, 61, 1, '2026-04-01'),
    (2, 1, 'RD12', 'Recurring Deposit',  'recurring', 750, 50,  'quarterly',   'on_maturity', 50000,  12, 60, 1, 100, 3, 23, 61, 1, '2026-04-01'),
    (3, 1, 'SB',   'Savings Deposit',    'savings',   400,  0,  'quarterly',   'quarterly',   10000,   0,  0, 1,   0, 0, 24, 61, 1, '2026-04-01');

-- ---------------------------------------------------------------------
-- Message templates. dlt_template_id MUST be filled from the DLT portal
-- before go-live — the SMS gateway refuses to send without it in
-- production, because the operator would silently drop the message.
-- ---------------------------------------------------------------------
-- SMS templates. dlt_template_id is deliberately left NULL: it must be
-- filled from the TRAI DLT portal before go-live, and GupshupSmsGateway
-- refuses to send without it in production, because the operator would
-- silently drop the message anyway.
INSERT IGNORE INTO message_templates (org_id, code, channel, language, body, wa_category, dlt_status, is_active) VALUES
    (1, 'emi_reminder_t3', 'sms', 'en', 'Dear {{member_name}}, your instalment of Rs {{amount}} on loan {{loan_no}} is due on {{due_date}}. Please pay on time to avoid charges. - {{lender}}', 'utility', 'not_registered', 1),
    (1, 'emi_reminder_t0', 'sms', 'en', 'Dear {{member_name}}, Rs {{amount}} is due TODAY on loan {{loan_no}}. - {{lender}}', 'utility', 'not_registered', 1),
    (1, 'receipt_ack',     'sms', 'en', 'Dear {{member_name}}, we have received Rs {{amount}} towards loan {{loan_no}} on {{date}}. Receipt {{receipt_no}}. Balance Rs {{balance}}. - {{lender}}', 'utility', 'not_registered', 1),
    (1, 'overdue_notice',  'sms', 'en', 'Dear {{member_name}}, Rs {{amount}} is overdue on loan {{loan_no}} ({{dpd}} days). Please contact your branch to regularise the account. - {{lender}}', 'utility', 'not_registered', 1),
    (1, 'otp_login',       'sms', 'en', '{{otp}} is your one-time password. Valid for 10 minutes. Do not share it with anyone. - {{lender}}', 'authentication', 'not_registered', 1);

-- WhatsApp templates track approval in wa_template_status, NOT dlt_status
-- (DLT governs SMS; Meta governs WhatsApp). Both must be filed as
-- UTILITY, never marketing — a payment reminder filed as marketing
-- triggers opt-out enforcement and is the wrong thing to send a
-- borrower in arrears.
INSERT IGNORE INTO message_templates
    (org_id, code, channel, language, body, wa_template_name, wa_category, wa_template_status, dlt_status, is_active) VALUES
    (1, 'sanction_letter', 'whatsapp', 'en', 'Dear {{member_name}}, your loan of Rs {{amount}} has been sanctioned. Your sanction letter is attached.', 'sanction_letter_v1', 'utility', 'not_submitted', 'not_registered', 1),
    (1, 'receipt_ack',     'whatsapp', 'ta', 'அன்புள்ள {{member_name}}, கடன் {{loan_no}}-க்கு ரூ {{amount}} பெறப்பட்டது. ரசீது {{receipt_no}}.', 'receipt_ack_ta_v1', 'utility', 'not_submitted', 'not_registered', 1);

-- ---------------------------------------------------------------------
-- Operational settings
-- ---------------------------------------------------------------------
INSERT IGNORE INTO settings (org_id, setting_key, setting_value, value_type, category, description) VALUES
    (1, 'npa_threshold_days',      '90',    'int',  'compliance', 'DPD at which an account becomes an NPA'),
    (1, 'bounce_charge_paise',     '50000', 'int',  'charges',    'Dishonour charge, excluding GST'),
    (1, 'accrual_accounting',      '1',     'bool', 'accounting', 'Accrue interest daily (accrual basis)'),
    (1, 'contact_window_start',    '8',     'int',  'compliance', 'Earliest hour a borrower may be contacted'),
    (1, 'contact_window_end',      '19',    'int',  'compliance', 'Latest hour a borrower may be contacted'),
    (1, 'max_cash_receipt_paise',  '20000000', 'int', 'compliance', 'Sec 269ST: no cash receipt of Rs 2,00,000 or more from one person in one day'),
    (1, 'bureau_cache_days',       '30',    'int',  'credit',     'Re-use a stored bureau report within this window'),
    (1, 'nidhi_deposit_nof_ratio', '20',    'int',  'compliance', 'Nidhi Rules r.14: deposits may not exceed 20x Net Owned Funds');

-- ---------------------------------------------------------------------
-- Bootstrap administrator
--   Username: admin        Password: ChangeMe#2026!
--   must_change_password = 1 forces a reset on first sign-in.
--   REGENERATE THIS HASH BEFORE ANY REAL DEPLOYMENT:
--     php -r "echo password_hash('your-password', PASSWORD_ARGON2ID);"
-- ---------------------------------------------------------------------
INSERT IGNORE INTO users
    (id, org_id, branch_id, employee_code, username, email, full_name, password_hash, password_algo,
     must_change_password, role_id, status)
VALUES
    (1, 1, 1, 'EMP001', 'admin', 'admin@example.in', 'System Administrator',
     '$argon2id$v=19$m=65536,t=4,p=2$Z0hGOWdsVkdCWTRpQ2FlSA$2MajwoHYp0OEyEMDiX9wiDjl6IjG06bBOgYaYX/ldH8',
     'argon2id', 1, 1, 'active');

-- ---------------------------------------------------------------------
-- Numbering series
-- ---------------------------------------------------------------------
INSERT IGNORE INTO number_sequences (org_id, branch_id, sequence_key, prefix, fy_year, current_value, padding) VALUES
    (1, NULL, 'member_no',      'MBR',  2026, 0, 5),
    (1, 2,    'loan_no',        'LN',   2026, 0, 5),
    (1, 2,    'application_no', 'APP',  2026, 0, 5),
    (1, 2,    'receipt_no',     'RCP',  2026, 0, 6),
    (1, 2,    'voucher_no',     'VCH',  2026, 0, 6);

-- ---------------------------------------------------------------------
-- Business day for today so postings are not blocked out of the box
-- ---------------------------------------------------------------------
INSERT IGNORE INTO business_days (org_id, branch_id, business_date, status, opened_at)
SELECT 1, id, CURDATE(), 'open', NOW() FROM branches WHERE org_id = 1;
