from django.core.management.base import BaseCommand
from courses.models import Course
from django.conf import settings
import os
from docx import Document

class Command(BaseCommand):
    help = 'Upload About BCOM content to the BCOM course.'

    def handle(self, *args, **options):
        course_slug = 'bcom'
        docx_path = os.path.join(settings.BASE_DIR, 'rawdocuments', 'About BCOM.docx')
        if not os.path.exists(docx_path):
            self.stdout.write(self.style.ERROR(f'DOCX file not found: {docx_path}'))
            return
        document = Document(docx_path)
        about_text = '\n'.join([para.text for para in document.paragraphs if para.text.strip()])
        # Add career prospects and certifications to overview
        career_prospects = [
            'Accountant', 'Financial Analyst', 'Auditor', 'Tax Consultant', 'Banking Officer',
            'Investment Analyst', 'Business Analyst', 'Management Trainee', 'Sales Executive/Marketing Coordinator',
            'Human Resources Assistant', 'Logistics Coordinator', 'Retail Store Manager Trainee',
            'Customer Service Representative', 'Entrepreneur/Startup Founder'
        ]
        certifications = [
            'CA (Chartered Accountant)', 'CPA (Certified Public Accountant)', 'CFA (Chartered Financial Analyst)',
            'CMA (Certified Management Accountant)', 'CIA (Certified Internal Auditor)', 'CFP (Certified Financial Planner)'
        ]
        extra_info = '\n\nCareer Prospects:\n' + '\n'.join(career_prospects) + '\n\nCertifications:\n' + '\n'.join(certifications)
        full_overview = about_text + '\n\n' + extra_info
        # SEO-friendly short description
        seo_short = (
            "Bachelor of Commerce (B.Com) at Haridwar University is a top-rated undergraduate program in India, "
            "covering finance, accounting, business management, and economics. Prepare for careers in banking, auditing, marketing, and more. "
            "Admissions open for 2025. Apply now!"
        )
        # SEO-friendly overview
        seo_overview = (
            "Enroll in the B.Com program at Haridwar University, Uttarakhand—one of India's best commerce degrees. "
            "Our curriculum blends financial accounting, business law, corporate finance, and marketing with hands-on internships and projects. "
            "Graduates excel in finance, accounting, marketing, management, and analytics. Career prospects include Accountant, Financial Analyst, Auditor, Tax Consultant, Banking Officer, Investment Analyst, Business Analyst, Management Trainee, Sales Executive, HR Assistant, Logistics Coordinator, Retail Manager, Customer Service, and Entrepreneur. "
            "Certifications: CA, CPA, CFA, CMA, CIA, CFP. Eligibility: 12th in any stream with 50% marks. Join a university focused on ethical business, global opportunities, and industry collaboration. Apply for B.Com admissions 2025 at Haridwar University!"
        )
        try:
            course = Course.objects.get(slug=course_slug)
        except Course.DoesNotExist:
            course = Course.objects.create(
                title='Bachelor of Commerce (BCOM)',
                slug=course_slug,
                short_description=seo_short,
                overview=seo_overview,
                duration='3 Years',
                category='Commerce',
            )
            self.stdout.write(self.style.SUCCESS('Course created.'))
        else:
            course.overview = seo_overview
            course.short_description = seo_short
            course.save()
            self.stdout.write(self.style.SUCCESS('Course updated.'))

        # Create or update eligibility
        from courses.models import CourseEligibility, CourseFee
        eligibility_text = '12th -in any stream with at least 50% marks from recognized board'
        try:
            eligibility = CourseEligibility.objects.get(course=course)
            eligibility.academic_qualification = eligibility_text
            eligibility.minimum_marks = '50%'
            eligibility.entrance_exam = 'None'
            eligibility.save()
            self.stdout.write(self.style.SUCCESS('Eligibility updated.'))
        except CourseEligibility.DoesNotExist:
            CourseEligibility.objects.create(
                course=course,
                academic_qualification=eligibility_text,
                minimum_marks='50%',
                entrance_exam='None'
            )
            self.stdout.write(self.style.SUCCESS('Eligibility created.'))

        # Create or update fee
        try:
            fee = CourseFee.objects.get(course=course)
            fee.domestic = 'Refer to official fee structure.'
            fee.international = 'Refer to official fee structure.'
            fee.save()
            self.stdout.write(self.style.SUCCESS('Fee updated.'))
        except CourseFee.DoesNotExist:
            CourseFee.objects.create(
                course=course,
                domestic='Refer to official fee structure.',
                international='Refer to official fee structure.'
            )
            self.stdout.write(self.style.SUCCESS('Fee created.'))

        self.stdout.write(self.style.SUCCESS('About BCOM content uploaded.'))
