import { PrismaClient, Role, Status } from '@prisma/client';
import * as bcrypt from 'bcrypt';

const prisma = new PrismaClient();

async function main() {
    const password = await bcrypt.hash('password123', 10);

    // Create Admin User
    await prisma.user.upsert({
        where: { email: 'admin@stihe.com' },
        update: {},
        create: {
            email: 'admin@stihe.com',
            fullName: 'Admin User',
            password,
            phone: '+123456789',
            academicProgram: 'Administration',
            role: Role.ADMIN,
            status: Status.APPROVED,
            emailVerified: true,
        },
    });

    // Create Student User
    const user = await prisma.user.upsert({
        where: { email: 'john@example.com' },
        update: {},
        create: {
            email: 'john@example.com',
            fullName: 'John Smith',
            password,
            phone: '+1234567890',
            academicProgram: 'Business Administration',
            level: 'Undergraduate',
            academicId: 'ST-2024-001',
            dob: new Date('2000-01-01'),
            gender: 'Male',
            role: Role.STUDENT,
            status: Status.PENDING,
        },
    });

    // Clean and Create Courses
    await prisma.enrollment.deleteMany({});
    await prisma.assignment.deleteMany({});
    await prisma.session.deleteMany({});
    await prisma.lessonCompletion.deleteMany({});
    await prisma.lesson.deleteMany({});
    await prisma.module.deleteMany({});
    await prisma.course.deleteMany({});

    const courses = [
        {
            code: 'ACC-101',
            title: 'Accountancy',
            instructor: 'Sarah Williams, CPA',
            category: 'Finance',
            credits: 3,
            level: 'Undergraduate',
            description: 'A comprehensive introduction to financial accounting. This course covers the fundamental principles of accounting, including the preparation of financial statements, the accounting cycle, and the analysis of business transactions. Students will gain practical skills in recording and interpreting financial data.',
            capacity: 50,
            isPublished: true,
            semester: 1,
            academicYear: '2023/2024',
            language: 'English',
            learningOutcomes: [
                'Understand and apply fundamental accounting principles',
                'Prepare comprehensive financial statements including Balance Sheets and P&L',
                'Master the complete double-entry bookkeeping cycle',
                'Analyze business transactions and their impact on financial health'
            ],
        },
        {
            code: 'LAW-101',
            title: 'Law',
            instructor: 'Atty. James Wilson',
            category: 'Legal',
            credits: 4,
            level: 'Undergraduate',
            description: 'An exploration of the fundamental concepts of law and legal systems. This course introduces students to constitutional law, torts, contracts, and criminal law. It emphasizes legal reasoning, case analysis, and the role of law in society, providing a solid foundation for future legal studies.',
            capacity: 40,
            isPublished: true,
            semester: 1,
            academicYear: '2025/2026',
            language: 'English',
            learningOutcomes: [
                'Identify and explain core legal frameworks and systems',
                'Analyze and interpret complex legal case studies',
                'Develop robust legal reasoning and argumentation skills',
                'Understand the intersection of law, ethics, and social policy'
            ],
        },
        {
            code: 'ECS-101',
            title: 'Early Childhood Studies',
            instructor: 'Dr. Maria Montessori',
            category: 'Education',
            credits: 3,
            level: 'Undergraduate',
            description: 'This course examines the theories and practices of early childhood education and development. Students will explore the physical, cognitive, social, and emotional growth of children from birth to age eight. Emphasis is placed on creating inclusive, engaging, and developmentally appropriate learning environments.',
            capacity: 30,
            isPublished: true,
            semester: 1,
            academicYear: '2025/2026',
            language: 'English',
            learningOutcomes: [
                'Understand key developmental milestones in early childhood',
                'Design and implement engaging, age-appropriate learning activities',
                'Apply major theories of child development in practical settings',
                'Foster inclusive and supportive environments for diverse learners'
            ],
        },
        {
            code: 'IT-101',
            title: 'Information Technology',
            instructor: 'Dr. Linus Torvalds',
            category: 'Technology',
            credits: 3,
            level: 'Undergraduate',
            description: 'A foundational course in Information Technology, covering computing systems, networking essentials, and software development methodologies. Students will learn the basics of hardware, operating systems, and building modern web applications, preparing them for a career in the rapidly evolving tech landscape.',
            capacity: 60,
            isPublished: true,
            semester: 1,
            academicYear: '2025/2026',
            language: 'English',
            learningOutcomes: [
                'Develop foundational programming skills in modern languages',
                'Understand and configure basic network architectures',
                'Master the basics of hardware and operating system management',
                'Implement software development best practices and version control'
            ],
        },
        {
            code: 'HSC-101',
            title: 'Health and Social Care',
            instructor: 'Florence Nightingale',
            category: 'Healthcare',
            credits: 3,
            level: 'Undergraduate',
            description: 'Focusing on the principles and practices of providing high-quality care in various health and social settings. This course covers communication skills, ethical considerations, and the importance of person-centered care. Students will gain insights into the healthcare system and the roles of care professionals.',
            capacity: 45,
            isPublished: true,
            semester: 1,
            academicYear: '2025/2026',
            language: 'English',
            learningOutcomes: [
                'Master person-centered caregiving techniques',
                'Apply ethical principles in healthcare decision-making',
                'Develop effective communication strategies for care settings',
                'Navigate the complex structures of modern health and social care systems'
            ],
        },
        {
            code: 'ENG-101',
            title: 'Engineering',
            instructor: 'Nikola Tesla',
            category: 'Engineering',
            credits: 4,
            level: 'Undergraduate',
            description: 'An introduction to the core principles of engineering, with a focus on mechanical and electrical systems. Students will apply physics and mathematical concepts to design and analyze engineering solutions. Topics include statics, dynamics, circuit analysis, and the engineering design process.',
            capacity: 35,
            isPublished: true,
            semester: 1,
            academicYear: '2025/2026',
            language: 'English',
            learningOutcomes: [
                'Apply physics and mathematics to solve engineering problems',
                'Perform detailed analysis of mechanical and electrical circuits',
                'Understand and execute the systematic engineering design process',
                'Develop technical drawing and modeling skills'
            ],
        },
        {
            code: 'FAT-101',
            title: 'Fashion and Textiles',
            instructor: 'Coco Chanel',
            category: 'Arts',
            credits: 3,
            level: 'Undergraduate',
            description: 'Exploring the dynamic world of fashion and textiles, from design and production to marketing and sustainability. Students will learn about garment construction, textile science, and the business of fashion. The course encourages creative expression alongside technical proficiency.',
            capacity: 25,
            isPublished: true,
            semester: 1,
            academicYear: '2023/2024',
            language: 'English',
            learningOutcomes: [
                'Identify and select appropriate textiles for design goals',
                'Master fundamental techniques in garment construction',
                'Understand fashion marketing and consumer behavior',
                'Apply sustainability principles to the fashion lifecycle'
            ],
        },
        {
            code: 'PM-101',
            title: 'Project Management',
            instructor: 'Henry Gantt',
            category: 'Business',
            credits: 3,
            level: 'Undergraduate',
            description: 'Equipping students with the methodologies and tools necessary for successful project delivery. Topics include project planning, risk management, team leadership, and Agile practices. This course provides a practical framework for managing complex projects within various industries.',
            capacity: 50,
            isPublished: true,
            semester: 1,
            academicYear: '2023/2024',
            language: 'English',
            learningOutcomes: [
                'Apply Agile and Waterfall project management methodologies',
                'Develop comprehensive project schedules and risk management plans',
                'Lead and motivate diverse project teams effectively',
                'Utilize modern project management software and tools'
            ],
        },
        {
            code: 'HAT-101',
            title: 'Hospitality and Tourism',
            instructor: 'Conrad Hilton',
            category: 'Hospitality',
            credits: 3,
            level: 'Undergraduate',
            description: 'A study of the management and operations within the hospitality and tourism industries. Students will explore hotel management, resort operations, and tourism services. Emphasis is placed on customer service excellence, marketing strategies, and the impact of tourism on local and global economies.',
            capacity: 40,
            isPublished: true,
            semester: 1,
            academicYear: '2023/2024',
            language: 'English',
            learningOutcomes: [
                'Deliver customer service excellence in hospitality settings',
                'Develop and implement tourism marketing strategies',
                'Understand the operational management of hotels and resorts',
                'Analyze the economic and social impacts of global tourism'
            ],
        },
    ];

    const createdCourses: any[] = [];
    for (const course of courses) {
        const c = await prisma.course.create({
            data: course,
        });
        createdCourses.push(c);
    }

    const course1 = createdCourses[0];
    const course2 = createdCourses[1];

    // Create Enrollments
    await prisma.enrollment.createMany({
        data: [
            { userId: user.id, courseId: course1.id, progress: 60 },
            { userId: user.id, courseId: course2.id, progress: 40 },
        ],
    });

    // Create Assignments
    await prisma.assignment.createMany({
        data: [
            {
                title: 'Spring 2026 Advanced UX Design New Adapters',
                dueDate: new Date('2026-04-01'),
                courseId: course1.id,
            },
            {
                title: 'Mobile Backend Architecture',
                dueDate: new Date('2026-04-15'),
                courseId: course2.id,
            },
        ],
    });

    // Create Tasks
    await prisma.task.deleteMany({});
    await prisma.task.createMany({
        data: [
            { title: 'Finish Agile assignment', progress: 40, userId: user.id },
            { title: 'Create DB model', progress: 100, status: 'Completed', userId: user.id },
        ],
    });

    // Create Announcements
    await prisma.announcement.deleteMany({});
    await prisma.announcement.createMany({
        data: [
            { title: 'Midterm exams schedule', content: 'The schedule for midterms is out.', userId: user.id },
            { title: 'New library resources', content: 'Check out the new UX resources.', userId: user.id },
        ],
    });

    // Create Payments
    await prisma.payment.deleteMany({});
    await prisma.payment.createMany({
        data: [
            { amount: 500.0, status: 'Completed', userId: user.id },
            { amount: 250.0, status: 'Pending', userId: user.id },
        ],
    });

    // Create System Config
    await prisma.systemConfig.deleteMany({});
    await prisma.systemConfig.create({
        data: {
            institutionName: 'STIHE Portal',
            primaryColor: '#6D28D9',
        },
    });

    console.log('Seed completed successfully');
}

main()
    .catch((e) => {
        console.error(e);
        process.exit(1);
    })
    .finally(async () => {
        await prisma.$disconnect();
    });
