#!/usr/bin/env python3
"""
Setup script for A+ Academy
Run this after installation to populate the database with sample subjects
"""

import os
import sys

# Add the parent directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from app import create_app, db
from app.models import Subject, Admin

def setup_database():
    app = create_app()

    with app.app_context():
        print("Creating database tables...")
        db.create_all()

        # Check if subjects already exist
        if Subject.query.first():
            print("Database already initialized with subjects.")
            return

        print("Adding sample subjects...")

        # Grade 10 Subjects
        grade_10_subjects = [
            Subject(name='Mathematics', code='MAT10', grade_level='10', 
                   description='Foundation mathematics covering algebra, geometry, and basic trigonometry.',
                   icon='bi-calculator', color='primary', display_order=1),
            Subject(name='Physical Sciences', code='PHY10', grade_level='10',
                   description='Introduction to physics and chemistry principles.',
                   icon='bi-flask', color='primary', display_order=2),
            Subject(name='Life Sciences', code='LIF10', grade_level='10',
                   description='Biology fundamentals including cell biology and ecology.',
                   icon='bi-heart-pulse', color='success', display_order=3),
            Subject(name='Computer Applications Technology', code='CAT10', grade_level='10',
                   description='Computer literacy, word processing, spreadsheets, and presentations.',
                   icon='bi-laptop', color='info', display_order=4),
        ]

        # Grade 11 Subjects
        grade_11_subjects = [
            Subject(name='Mathematics', code='MAT11', grade_level='11',
                   description='Advanced algebra, calculus foundations, and analytical geometry.',
                   icon='bi-calculator', color='primary', display_order=1),
            Subject(name='Physical Sciences', code='PHY11', grade_level='11',
                   description='Mechanics, waves, matter and materials, chemical change.',
                   icon='bi-flask', color='primary', display_order=2),
            Subject(name='Life Sciences', code='LIF11', grade_level='11',
                   description='Human physiology, genetics, and evolution.',
                   icon='bi-heart-pulse', color='success', display_order=3),
            Subject(name='Computer Applications Technology', code='CAT11', grade_level='11',
                   description='Advanced Office applications, databases, and web design basics.',
                   icon='bi-laptop', color='info', display_order=4),
        ]

        # Grade 12 Subjects
        grade_12_subjects = [
            Subject(name='Mathematics', code='MAT12', grade_level='12',
                   description='Calculus, probability, financial mathematics, and Euclidean geometry.',
                   icon='bi-calculator', color='primary', display_order=1),
            Subject(name='Physical Sciences', code='PHY12', grade_level='12',
                   description='Momentum, work-energy-power, electrodynamics, chemical equilibrium.',
                   icon='bi-flask', color='primary', display_order=2),
            Subject(name='Life Sciences', code='LIF12', grade_level='12',
                   description='DNA replication, protein synthesis, human reproduction, biotechnology.',
                   icon='bi-heart-pulse', color='success', display_order=3),
            Subject(name='Computer Applications Technology', code='CAT12', grade_level='12',
                   description='Advanced databases, HTML/CSS, information management, and network basics.',
                   icon='bi-laptop', color='info', display_order=4),
        ]

        # Rewrite/Upgrade Subjects
        rewrite_subjects = [
            Subject(name='Mathematics Rewrite', code='MAT-RW', grade_level='rewrite',
                   description='Intensive revision for students rewriting Mathematics. Focus on exam technique and problem-solving.',
                   icon='bi-arrow-clockwise', color='warning', display_order=1),
            Subject(name='Physical Sciences Rewrite', code='PHY-RW', grade_level='rewrite',
                   description='Comprehensive revision covering all Grade 12 Physics and Chemistry topics.',
                   icon='bi-arrow-clockwise', color='warning', display_order=2),
            Subject(name='Life Sciences Rewrite', code='LIF-RW', grade_level='rewrite',
                   description='Complete syllabus revision with focus on past exam papers and answering techniques.',
                   icon='bi-arrow-clockwise', color='warning', display_order=3),
            Subject(name='CAT Upgrade', code='CAT-UP', grade_level='rewrite',
                   description='Upgrade your CAT marks with practical projects and theory revision.',
                   icon='bi-arrow-up-circle', color='warning', display_order=4),
        ]

        all_subjects = grade_10_subjects + grade_11_subjects + grade_12_subjects + rewrite_subjects

        for subject in all_subjects:
            db.session.add(subject)

        db.session.commit()
        print(f"Added {len(all_subjects)} subjects successfully!")

        # Display admin credentials reminder
        print("\n" + "="*50)
        print("IMPORTANT: Default Admin Credentials")
        print("="*50)
        print("Username: admin")
        print("Password: admin123")
        print("\nPlease change these credentials immediately after first login!")
        print("="*50)

if __name__ == '__main__':
    setup_database()
    print("\nSetup complete! Run 'python run.py' to start the server.")
