<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class JobCategoriesAndSkillsSeeder extends Seeder
{
    public function run(): void
    {
        $this->seedCategories();
        $this->seedSkills();
    }

    private function seedCategories(): void
    {
        $categories = [
            'Information Technology' => ['Software Development', 'DevOps & Infrastructure', 'Data & Analytics', 'QA & Testing', 'UI/UX Design'],
            'Sales & Marketing'      => ['Digital Marketing', 'Business Development', 'Customer Success', 'Content & Social Media'],
            'Finance & Accounting'   => ['Accounting', 'Financial Analysis', 'Audit & Compliance'],
            'Operations'             => ['Supply Chain', 'Project Management', 'Administration'],
            'Human Resources'        => ['Recruitment', 'HR Operations', 'Training & Development'],
            'Customer Support'       => ['Technical Support', 'Call Center'],
            'Healthcare'             => ['Nursing', 'Medical Practice', 'Pharmacy'],
            'Education'              => ['Teaching', 'Academic Administration'],
        ];

        foreach ($categories as $parentName => $children) {
            $parentSlug = url_title($parentName, '-', true);
            $existing = $this->db->table('job_categories')->where('slug', $parentSlug)->get()->getRow();

            if ($existing) {
                $parentId = $existing->id;
            } else {
                $this->db->table('job_categories')->insert(['name' => $parentName, 'slug' => $parentSlug]);
                $parentId = $this->db->insertID();
            }

            foreach ($children as $childName) {
                $childSlug = url_title($childName, '-', true);
                $childExists = $this->db->table('job_categories')->where('slug', $childSlug)->get()->getRow();
                if (! $childExists) {
                    $this->db->table('job_categories')->insert([
                        'name'      => $childName,
                        'slug'      => $childSlug,
                        'parent_id' => $parentId,
                    ]);
                }
            }
        }
    }

    private function seedSkills(): void
    {
        $skills = [
            'Programming'  => ['PHP', 'JavaScript', 'Python', 'Java', 'C#', 'TypeScript', 'Go', 'Ruby', 'Swift', 'Kotlin'],
            'Frameworks'   => ['Laravel', 'CodeIgniter', 'React', 'Vue.js', 'Angular', 'Node.js', 'Django', 'Spring Boot', '.NET'],
            'Databases'    => ['MySQL', 'PostgreSQL', 'MongoDB', 'Redis', 'SQL Server'],
            'Design'       => ['Figma', 'Adobe Photoshop', 'Adobe Illustrator', 'UI Design', 'UX Research'],
            'Marketing'    => ['SEO', 'Google Ads', 'Facebook Ads', 'Content Writing', 'Email Marketing'],
            'Soft Skills'  => ['Communication', 'Team Leadership', 'Problem Solving', 'Project Management'],
            'Data'         => ['Excel', 'Power BI', 'Tableau', 'SQL', 'Data Analysis'],
            'DevOps'       => ['Docker', 'Kubernetes', 'AWS', 'CI/CD', 'Linux Administration'],
        ];

        foreach ($skills as $category => $names) {
            foreach ($names as $name) {
                $exists = $this->db->table('skills')->where('name', $name)->get()->getRow();
                if (! $exists) {
                    $this->db->table('skills')->insert(['name' => $name, 'category' => $category]);
                }
            }
        }
    }
}
