<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $dependencies = [
        // Member module
        ['member.edit', 'member.view'],
        ['member.archive', 'member.view'],
        ['member.change_status', 'member.view'],
        ['member.pay_form_fee', 'member.view'],
        ['member.pay_membership', 'member.view'],
        ['member.fill_form', 'member.view'],
        ['member.view_financial', 'member.view'],

        // Spouse/Child/Temp
        ['spouse.add', 'spouse.view'],
        ['spouse.edit', 'spouse.view'],
        ['spouse.remove', 'spouse.view'],
        ['child.add', 'child.view'],
        ['child.edit', 'child.view'],
        ['child.remove', 'child.view'],
        ['temp.add', 'temp.view'],
        ['temp.edit', 'temp.view'],
        ['temp.remove', 'temp.view'],

        // Payments
        ['payment.process_cash', 'payment.view'],
        ['payment.process_check', 'payment.view'],
        ['payment.process_visa', 'payment.view'],
        ['payment.void_receipt', 'payment.view'],
        ['payment.refund', 'payment.view'],

        // Accounting
        ['accounting.journal.create', 'accounting.journal.view'],
        ['accounting.journal.post', 'accounting.journal.view'],
        ['accounting.journal.reverse', 'accounting.journal.view'],
        ['accounting.coa.manage', 'accounting.coa.view'],
        ['accounting.fiscal_year.manage', 'accounting.fiscal_year.view'],
        ['accounting.fiscal_year.close', 'accounting.fiscal_year.view'],
        ['accounting.period.close', 'accounting.period.view'],
        ['accounting.period.reopen', 'accounting.period.view'],
        ['accounting.budget.manage', 'accounting.budget.view'],
        ['accounting.bank_account.manage', 'accounting.bank_account.view'],
        ['accounting.bank_recon.manage', 'accounting.bank_recon.view'],
        ['accounting.settlements.manage', 'accounting.settlements.view'],
        ['accounting.settlements.approve', 'accounting.settlements.view'],

        // HR
        ['hr.employee.manage', 'hr.employee.view'],
        ['hr.employee.manage_salary', 'hr.employee.view_salary'],
        ['hr.attendance.manage', 'hr.attendance.view'],
        ['hr.attendance.approve', 'hr.attendance.view'],
        ['hr.leave.manage', 'hr.leave.view'],
        ['hr.leave.approve', 'hr.leave.view'],
        ['hr.payroll.process', 'hr.payroll.view'],
        ['hr.payroll.approve', 'hr.payroll.view'],
        ['hr.loan.manage', 'hr.loan.view'],
        ['hr.loan.approve', 'hr.loan.view'],
        ['hr.overtime.manage', 'hr.overtime.view'],
        ['hr.overtime.approve', 'hr.overtime.view'],
        ['hr.eos.manage', 'hr.eos.view'],
        ['hr.eos.approve', 'hr.eos.view'],

        // Sports
        ['sa.discipline.manage', 'sa.discipline.view'],
        ['sa.facility.manage', 'sa.facility.view'],
        ['sa.coach.manage', 'sa.coach.view'],
        ['sa.player.manage', 'sa.player.view'],
        ['sa.academy.manage', 'sa.academy.view'],
        ['sa.group.manage', 'sa.group.view'],
        ['sa.group.enroll', 'sa.group.view'],
        ['sa.schedule.manage', 'sa.schedule.view'],
        ['sa.booking.manage', 'sa.booking.view'],
        ['sa.pricing.manage', 'sa.pricing.view'],
        ['sa.subscription.collect', 'sa.subscription.view'],
        ['sa.subscription.exempt', 'sa.subscription.view'],
        ['sa.attendance.manage', 'sa.attendance.view'],

        // Subscriptions/Installments/Fines
        ['subscription.collect', 'subscription.view'],
        ['subscription.exempt', 'subscription.view'],
        ['subscription.generate_batch', 'subscription.view'],
        ['installment.create_plan', 'installment.view'],
        ['installment.record_payment', 'installment.view'],
        ['fine.impose', 'fine.view'],
        ['fine.collect', 'fine.view'],
        ['fine.waive', 'fine.view'],

        // Transfers/Waivers
        ['transfer.initiate', 'transfer.view'],
        ['transfer.approve', 'transfer.view'],
        ['waiver.initiate', 'waiver.view'],
        ['waiver.approve', 'waiver.view'],

        // Users/Roles
        ['user.create', 'user.view'],
        ['user.edit', 'user.view'],
        ['user.assign_role', 'user.view'],
        ['user.manage_permissions', 'user.view'],
        ['role.create', 'role.view'],
        ['role.edit', 'role.view'],

        // Inventory
        ['inventory.manage', 'inventory.view'],
        ['warehouse.manage', 'warehouse.view'],
        ['stock.move', 'stock.view'],
        ['stock.approve_transfer', 'stock.view'],
        ['purchase.create', 'purchase.view'],
        ['purchase.approve', 'purchase.view'],

        // Others
        ['reservation.create', 'reservation.view'],
        ['reservation.confirm', 'reservation.view'],
        ['reservation.cancel', 'reservation.view'],
        ['carnet.issue', 'carnet.view'],
        ['carnet.deactivate', 'carnet.view'],
        ['document.upload', 'document.view'],
        ['document.delete', 'document.view'],
        ['interview.schedule', 'interview.view'],
        ['interview.decide', 'interview.view'],
        ['settings.edit', 'settings.view'],
    ];

    foreach ($dependencies as [$permKey, $reqKey]) {
        $exists = $db->selectOne(
            "SELECT 1 FROM permission_dependencies WHERE permission_key = ? AND requires_key = ?",
            [$permKey, $reqKey]
        );
        if (!$exists) {
            $db->insert('permission_dependencies', [
                'permission_key' => $permKey,
                'requires_key'   => $reqKey,
                'is_auto_grant'  => 1,
            ]);
        }
    }
};
