from django.core.management.base import BaseCommand

from subscriptions.models import SubscriptionPlan

C = SubscriptionPlan.Category
A = SubscriptionPlan.Audience

PLANS = [
    # ── Job seeker membership ──────────────────────────────────────────
    {
        'slug': 'seeker-free-professional',
        'name': 'Free Professional Membership',
        'audience': A.SEEKER,
        'category': C.MEMBERSHIP,
        'price_monthly': 0,
        'price_label': 'Forever free',
        'sort_order': 1,
        'description': 'Perfect for every professional starting their career journey.',
        'features': (
            'Create a professional profile\n'
            'Upload your CV\n'
            'Professional profile URL\n'
            'Be searchable by employers\n'
            'Basic profile analytics\n'
            'Apply for jobs\n'
            'Save jobs\n'
            'Basic job alerts\n'
            'Follow companies'
        ),
    },
    {
        'slug': 'seeker-professional-plus',
        'name': 'Professional Plus',
        'audience': A.SEEKER,
        'category': C.MEMBERSHIP,
        'price_monthly': 20,
        'price_label': '/month, billed monthly',
        'sort_order': 2,
        'is_featured': True,
        'description': 'Stand out to employers and unlock premium career tools.',
        'features': (
            'Everything in Free, plus:\n'
            'Increased visibility\n'
            'Featured Professional Badge\n'
            'Higher ranking in employer search results\n'
            'Priority placement in the Talent Database\n'
            'Profile highlighted to recruiters\n'
            'Job recommendations\n'
            'Priority job alerts\n'
            'Unlimited saved jobs\n'
            'Application history\n'
            'See who viewed your profile\n'
            'See who downloaded your CV\n'
            'Receive notifications when recruiters shortlist you\n'
            'Track profile performance\n'
            'Follow professionals\n'
            'Follow companies'
        ),
    },
    # ── Employer recruitment plans ─────────────────────────────────────
    {
        'slug': 'company-starter',
        'name': 'Starter',
        'audience': A.COMPANY,
        'category': C.RECRUITMENT,
        'price_monthly': 500,
        'price_label': '/month, billed monthly',
        'sort_order': 1,
        'description': 'Essential tools for small teams hiring occasionally.',
        'features': (
            'Company profile\n'
            'Up to 2 active job adverts per month\n'
            'Search up to 5 candidates'
        ),
    },
    {
        'slug': 'company-professional',
        'name': 'Professional',
        'audience': A.COMPANY,
        'category': C.RECRUITMENT,
        'price_monthly': 1500,
        'price_label': '/month, billed monthly',
        'sort_order': 2,
        'is_featured': True,
        'description': 'For growing companies serious about finding the best talent.',
        'features': (
            'Company profile\n'
            'Up to 5 active job adverts per month\n'
            '2 social media promotions\n'
            'Search up to 20 CVs per month\n'
            'Download CVs\n'
            'Save shortlisted candidates\n'
            'Message system'
        ),
    },
    {
        'slug': 'company-recruiter-pro',
        'name': 'Recruiter Pro',
        'audience': A.COMPANY,
        'category': C.RECRUITMENT,
        'price_monthly': 5000,
        'price_label': '/month, billed monthly',
        'sort_order': 3,
        'description': 'High-volume hiring with promotions and bulk outreach.',
        'features': (
            'Company profile\n'
            'Homepage feature\n'
            'Unlimited job adverts\n'
            'Unlimited CV searches\n'
            '100 CV downloads\n'
            'Four social media promotions\n'
            'Easy Apply\n'
            'Bulk communication'
        ),
    },
    {
        'slug': 'company-enterprise',
        'name': 'Enterprise',
        'audience': A.COMPANY,
        'category': C.RECRUITMENT,
        'price_monthly': 8000,
        'price_label': '/month, billed monthly',
        'sort_order': 4,
        'description': 'Full recruitment suite with insights and premium visibility.',
        'features': (
            'Everything in Recruiter Pro\n'
            'Featured jobs\n'
            'Job alert email distribution\n'
            'Google job visibility\n'
            'Advanced recruitment dashboard\n'
            'Recruitment insights\n'
            'Candidate ratings and notes'
        ),
    },
    # ── Talent database / CV search add-ons ────────────────────────────
    {
        'slug': 'talent-cv-basic',
        'name': 'CV Search Basic',
        'audience': A.COMPANY,
        'category': C.TALENT_DATABASE,
        'price_monthly': 2000,
        'price_label': '/month, billed monthly',
        'sort_order': 1,
        'description': 'Ideal for employers with occasional recruitment needs.',
        'features': (
            'Search up to 40 candidate profiles\n'
            'Download up to 40 CVs\n'
            'Send direct messages to candidates\n'
            'Receive notifications when new matching candidates join the platform'
        ),
    },
    {
        'slug': 'talent-cv-plus',
        'name': 'CV Search Plus',
        'audience': A.COMPANY,
        'category': C.TALENT_DATABASE,
        'price_monthly': 5000,
        'price_label': '/month, billed monthly',
        'sort_order': 2,
        'is_featured': True,
        'description': 'Designed for businesses recruiting regularly.',
        'features': (
            'Search up to 100 candidate profiles\n'
            'Download up to 100 CVs\n'
            'Advanced search and filtering\n'
            'Send individual or bulk messages to candidates\n'
            'Automated email reminders to unresponsive candidates\n'
            'Receive notifications when new matching candidates join the platform'
        ),
    },
    {
        'slug': 'talent-cv-unlimited',
        'name': 'CV Search Unlimited',
        'audience': A.COMPANY,
        'category': C.TALENT_DATABASE,
        'price_monthly': 8000,
        'price_label': '/month, billed monthly',
        'sort_order': 3,
        'description': 'For organisations with ongoing or high-volume recruitment needs.',
        'features': (
            'Unlimited candidate searches\n'
            'Unlimited CV downloads\n'
            'Advanced search and filtering\n'
            'Save and bookmark candidate profiles\n'
            'Send individual or bulk messages to candidates\n'
            'Automated email reminders to unresponsive candidates\n'
            'Receive notifications when new matching candidates join the platform\n'
            'Priority access to newly registered professionals'
        ),
    },
]

ACTIVE_SLUGS = [p['slug'] for p in PLANS]


class Command(BaseCommand):
    help = 'Seed subscription plans'

    def handle(self, *args, **options):
        for data in PLANS:
            SubscriptionPlan.objects.update_or_create(
                slug=data['slug'],
                defaults={**data, 'is_active': True},
            )
        deactivated = SubscriptionPlan.objects.exclude(slug__in=ACTIVE_SLUGS).update(is_active=False)
        self.stdout.write(self.style.SUCCESS(
            f'Seeded {len(PLANS)} plans ({SubscriptionPlan.objects.filter(is_active=True).count()} active, '
            f'{deactivated} deactivated).'
        ))
