from django.core.management.base import BaseCommand

from jobs.models import Job, JobCategory


SEED_JOBS = [
    {
        'slug': 'senior-software-engineer-airtel',
        'title': 'Senior Software Engineer',
        'company_name': 'Airtel Zambia',
        'company_initials': 'AT',
        'company_logo_gradient': 'linear-gradient(135deg,#e53935,#b71c1c)',
        'company_industry': 'Telecommunications',
        'location': 'Lusaka, Zambia',
        'job_type': Job.JobType.FULL_TIME,
        'experience_level': Job.ExperienceLevel.SENIOR,
        'category': 'information-technology-software',
        'salary_min': 18000,
        'salary_max': 25000,
        'badge': Job.Badge.HOT,
        'is_featured': True,
        'cover_image_url': 'https://images.unsplash.com/photo-1497366216548-37526070297c?w=1200&q=80',
        'description': (
            "Airtel Zambia is seeking an experienced Senior Software Engineer to join our Technology team in Lusaka. "
            "You will be responsible for architecting, developing, and maintaining scalable software solutions "
            "that power Airtel's digital services for millions of customers across Zambia."
        ),
        'responsibilities': '\n'.join([
            'Design and develop high-performance backend systems using Python, Java, or Node.js',
            'Lead technical architecture discussions and mentor junior engineers',
            'Collaborate with product teams to deliver features on time and at scale',
            'Optimize application performance for millions of concurrent users',
            'Implement CI/CD pipelines and DevOps best practices',
        ]),
        'requirements': '\n'.join([
            "Bachelor's degree in Computer Science or related field",
            'Minimum 5 years of professional software development experience',
            'Strong proficiency in Python, Java, or Node.js',
            'Experience with cloud platforms (AWS, GCP, or Azure)',
            'Solid understanding of microservices architecture and RESTful APIs',
        ]),
        'nice_to_have': '\n'.join([
            'Experience in the telecommunications industry',
            'Knowledge of mobile money APIs (Airtel Money, MTN MoMo)',
        ]),
        'benefits': '\n'.join([
            'Medical Aid',
            'Pension Fund',
            'Study Assistance',
            'Work Laptop',
            '25 Days Annual Leave',
            'Performance Bonuses',
        ]),
        'how_to_apply': (
            'Click Apply Now to submit your application through PEZAJOB. '
            'Ensure you upload your updated CV and a tailored cover letter.'
        ),
    },
    {
        'slug': 'financial-analyst-zanaco',
        'title': 'Financial Analyst',
        'company_name': 'ZANACO Bank',
        'company_initials': 'ZA',
        'company_logo_gradient': 'linear-gradient(135deg,#2e7d32,#1b5e20)',
        'company_industry': 'Banking & Finance',
        'location': 'Lusaka, Zambia',
        'job_type': Job.JobType.FULL_TIME,
        'experience_level': Job.ExperienceLevel.MID,
        'category': 'banking-insurance-financial-services',
        'salary_min': 12000,
        'salary_max': 16000,
        'badge': Job.Badge.NEW,
        'description': 'ZANACO Bank is looking for a detail-oriented Financial Analyst to support corporate banking reporting and forecasting.',
        'responsibilities': '\n'.join([
            'Prepare monthly financial reports and variance analysis',
            'Support budgeting and forecasting processes',
            'Monitor KPIs for corporate banking portfolios',
        ]),
        'requirements': '\n'.join([
            'Degree in Finance, Accounting, or Economics',
            '3+ years experience in banking or financial analysis',
            'Advanced Excel and financial modelling skills',
        ]),
        'benefits': '\n'.join(['Medical Cover', 'Performance Bonus', 'Training Programs']),
    },
    {
        'slug': 'network-operations-engineer-mtn',
        'title': 'Network Operations Engineer',
        'company_name': 'MTN Zambia',
        'company_initials': 'MT',
        'company_logo_gradient': 'linear-gradient(135deg,#f9a825,#e65100)',
        'company_industry': 'Telecommunications',
        'location': 'Lusaka, Zambia',
        'job_type': Job.JobType.FULL_TIME,
        'experience_level': Job.ExperienceLevel.MID,
        'category': 'information-technology-software',
        'salary_min': 14000,
        'salary_max': 18000,
        'badge': Job.Badge.PREMIUM,
        'description': 'Join MTN Zambia to monitor, maintain and optimize core network infrastructure across Zambia.',
        'responsibilities': '\n'.join([
            'Monitor network performance and resolve incidents',
            'Coordinate with vendors on maintenance windows',
            'Document network changes and standard operating procedures',
        ]),
        'requirements': '\n'.join([
            'Degree in Telecommunications, IT or Engineering',
            'CCNA or equivalent certification',
            '3+ years in NOC or network engineering roles',
        ]),
        'benefits': '\n'.join(['Medical Aid', 'Phone & Data Allowance', 'Career Growth']),
    },
    {
        'slug': 'branch-manager-corporate-banking-zanaco',
        'title': 'Branch Manager – Corporate Banking',
        'company_name': 'ZANACO Bank',
        'company_initials': 'ZA',
        'company_logo_gradient': 'linear-gradient(135deg,#2e7d32,#1b5e20)',
        'company_industry': 'Banking & Finance',
        'location': 'Lusaka, Zambia',
        'job_type': Job.JobType.FULL_TIME,
        'experience_level': Job.ExperienceLevel.SENIOR,
        'category': 'banking-insurance-financial-services',
        'salary_min': 22000,
        'salary_max': 32000,
        'badge': Job.Badge.NEW,
        'description': 'Lead corporate banking operations and relationship management for key business clients.',
        'responsibilities': '\n'.join([
            'Manage branch corporate portfolio and revenue targets',
            'Lead and coach relationship managers',
            'Ensure compliance with banking regulations',
        ]),
        'requirements': '\n'.join([
            '7+ years banking experience with leadership exposure',
            'Strong knowledge of corporate banking products',
            'Excellent stakeholder management skills',
        ]),
        'benefits': '\n'.join(['Company Car', 'Medical Aid', 'Annual Bonus']),
    },
    {
        'slug': 'electrical-engineer-zesco',
        'title': 'Electrical Engineer – Power Distribution',
        'company_name': 'ZESCO',
        'company_initials': 'ZE',
        'company_logo_gradient': 'linear-gradient(135deg,#1565c0,#0d47a1)',
        'company_industry': 'Energy & Utilities',
        'location': 'Lusaka, Zambia',
        'job_type': Job.JobType.FULL_TIME,
        'experience_level': Job.ExperienceLevel.MID,
        'category': 'engineering',
        'salary_min': 12000,
        'salary_max': 18000,
        'badge': Job.Badge.PREMIUM,
        'description': 'Support design, maintenance and optimization of ZESCO power distribution systems.',
        'responsibilities': '\n'.join([
            'Inspect and maintain distribution infrastructure',
            'Prepare technical reports and project documentation',
            'Collaborate with field teams on outage response',
        ]),
        'requirements': '\n'.join([
            'BEng in Electrical Engineering',
            '3+ years power distribution experience',
            'Knowledge of Zambian grid standards',
        ]),
        'benefits': '\n'.join(['Housing Allowance', 'Medical Cover', 'Pension']),
    },
    {
        'slug': 'cloud-solutions-architect-liquid',
        'title': 'Cloud Solutions Architect',
        'company_name': 'Liquid Intelligent Tech',
        'company_initials': 'LI',
        'company_logo_gradient': 'linear-gradient(135deg,#0277bd,#01579b)',
        'company_industry': 'Technology',
        'location': 'Lusaka, Zambia',
        'job_type': Job.JobType.FULL_TIME,
        'experience_level': Job.ExperienceLevel.SENIOR,
        'category': 'information-technology-software',
        'salary_min': 24000,
        'salary_max': 35000,
        'badge': Job.Badge.PREMIUM,
        'description': 'Design and implement cloud solutions for enterprise clients across Africa.',
        'responsibilities': '\n'.join([
            'Architect secure, scalable cloud environments',
            'Lead pre-sales technical discovery with clients',
            'Mentor engineering teams on cloud best practices',
        ]),
        'requirements': '\n'.join([
            '5+ years cloud architecture experience',
            'AWS/Azure certifications preferred',
            'Strong client-facing communication skills',
        ]),
        'benefits': '\n'.join(['Remote Flexibility', 'Certification Budget', 'Medical Aid']),
    },
    {
        'slug': 'risk-compliance-officer-fnb',
        'title': 'Risk & Compliance Officer',
        'company_name': 'FNB Zambia',
        'company_initials': 'FN',
        'company_logo_gradient': 'linear-gradient(135deg,#6a1b9a,#4a148c)',
        'company_industry': 'Banking & Finance',
        'location': 'Lusaka, Zambia',
        'job_type': Job.JobType.FULL_TIME,
        'experience_level': Job.ExperienceLevel.MID,
        'category': 'banking-insurance-financial-services',
        'salary_min': 15000,
        'salary_max': 22000,
        'badge': Job.Badge.NEW,
        'description': 'Ensure FNB Zambia operations meet regulatory and internal compliance standards.',
        'responsibilities': '\n'.join([
            'Monitor compliance with BoZ regulations',
            'Conduct risk assessments and audits',
            'Prepare regulatory reporting',
        ]),
        'requirements': '\n'.join([
            'Degree in Law, Finance or Risk Management',
            '4+ years compliance experience in banking',
            'Knowledge of AML/KYC frameworks',
        ]),
        'benefits': '\n'.join(['Medical Aid', 'Professional Memberships', 'Bonus Scheme']),
    },
    {
        'slug': 'graduate-trainee-shoprite',
        'title': 'Graduate Management Trainee',
        'company_name': 'Shoprite Zambia',
        'company_initials': 'SH',
        'company_logo_gradient': 'linear-gradient(135deg,#d32f2f,#b71c1c)',
        'company_industry': 'Retail & FMCG',
        'location': 'Lusaka, Zambia',
        'job_type': Job.JobType.INTERNSHIP,
        'experience_level': Job.ExperienceLevel.ENTRY,
        'category': 'retail-consumer-services',
        'salary_min': 5000,
        'salary_max': 8000,
        'badge': Job.Badge.HOT,
        'description': '12-month graduate programme across store operations, supply chain and finance.',
        'responsibilities': '\n'.join([
            'Rotate through key business functions',
            'Support store and regional management teams',
            'Complete assigned training modules',
        ]),
        'requirements': '\n'.join([
            'Recent graduate with minimum second-class degree',
            'Strong analytical and communication skills',
            'Willingness to work across Zambia',
        ]),
        'benefits': '\n'.join(['Training Programme', 'Staff Discount', 'Mentorship']),
    },
]


class Command(BaseCommand):
    help = 'Seed sample job listings (requires seed_job_categories first)'

    def handle(self, *args, **options):
        category_map = {
            c.slug: c for c in JobCategory.objects.filter(is_active=True)
        }
        if not category_map:
            self.stderr.write(
                self.style.WARNING('No job categories found — run seed_job_categories first.')
            )

        created = 0
        updated = 0
        for data in SEED_JOBS:
            category_slug = data.pop('category')
            category = category_map.get(category_slug)
            slug = data['slug']
            job, was_created = Job.objects.update_or_create(
                slug=slug,
                defaults={**data, 'category': category, 'is_active': True},
            )
            if was_created:
                created += 1
            else:
                updated += 1

        self.stdout.write(
            self.style.SUCCESS(
                f'Seed complete: {created} created, {updated} updated '
                f'({Job.objects.count()} total jobs)'
            )
        )
