<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $departments = [
        ['code' => 'EXEC', 'name_ar' => 'الإدارة العليا', 'name_en' => 'Executive Management'],
        ['code' => 'TECH', 'name_ar' => 'المكتب الفني', 'name_en' => 'Technical Office'],
        ['code' => 'SPORT', 'name_ar' => 'ادارة النشاط الرياضي', 'name_en' => 'Sports Activities'],
        ['code' => 'PR', 'name_ar' => 'ادارة العلاقات العامة', 'name_en' => 'Public Relations'],
        ['code' => 'FIN', 'name_ar' => 'الادارة المالية', 'name_en' => 'Finance'],
        ['code' => 'STORE', 'name_ar' => 'ادارة المخازن', 'name_en' => 'Warehouse'],
        ['code' => 'HR', 'name_ar' => 'ادارة الموارد البشرية', 'name_en' => 'Human Resources'],
        ['code' => 'LEGAL', 'name_ar' => 'ادارة الشئون القانونية', 'name_en' => 'Legal Affairs'],
        ['code' => 'SUPER', 'name_ar' => 'ادارة الاشراف والمتابعة', 'name_en' => 'Supervision & Follow-up'],
        ['code' => 'MKTG', 'name_ar' => 'ادارة التسويق والمبيعات', 'name_en' => 'Marketing & Sales'],
        ['code' => 'MEMB', 'name_ar' => 'ادارة اشتراكات العضوية', 'name_en' => 'Membership Subscriptions'],
        ['code' => 'IT', 'name_ar' => 'ادارة النظم والمعلومات', 'name_en' => 'IT & Systems'],
        ['code' => 'MED', 'name_ar' => 'الادارة الطبية', 'name_en' => 'Medical'],
        ['code' => 'MOSQ', 'name_ar' => 'المسجد', 'name_en' => 'Mosque'],
    ];

    foreach ($departments as $dept) {
        $existing = $db->selectOne(
            "SELECT id FROM hr_departments WHERE department_code = ? AND is_archived = 0",
            [$dept['code']]
        );
        if (!$existing) {
            $db->insert('hr_departments', [
                'department_code' => $dept['code'],
                'department_type' => 'idara',
                'name_ar'         => $dept['name_ar'],
                'name_en'         => $dept['name_en'],
                'parent_id'       => null,
                'is_active'       => 1,
                'sort_order'      => 0,
                'created_at'      => date('Y-m-d H:i:s'),
                'updated_at'      => date('Y-m-d H:i:s'),
            ]);
        }
    }

    // Extract distinct job titles from the registry JSON
    $jsonPath = __DIR__ . '/data/employees_registry.json';
    if (!file_exists($jsonPath)) {
        return;
    }

    $employees = json_decode(file_get_contents($jsonPath), true);
    $titles = [];
    foreach ($employees as $emp) {
        $title = trim($emp['job_title'] ?? '');
        if ($title && !in_array($title, $titles, true)) {
            $titles[] = $title;
        }
    }

    $codeCounter = 1;
    foreach ($titles as $title) {
        $existing = $db->selectOne(
            "SELECT id FROM hr_job_titles WHERE name_ar = ? AND is_archived = 0",
            [$title]
        );
        if (!$existing) {
            $code = 'JOB' . str_pad((string) $codeCounter, 3, '0', STR_PAD_LEFT);
            $db->insert('hr_job_titles', [
                'title_code' => $code,
                'name_ar'    => $title,
                'name_en'    => null,
                'is_active'  => 1,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
        }
        $codeCounter++;
    }
};
