<?php

namespace Database\Seeders;

use App\Models\AuditLog;
use App\Models\Certificate;
use App\Models\CertificateTemplate;
use App\Models\College;
use App\Models\Course;
use App\Models\Student;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // 1. Seed Users
        $superAdmin = User::updateOrCreate(
            ['email' => 'admin@humanitywelfare.org'],
            [
                'name' => 'Humanity Welfare Super Admin',
                'password' => Hash::make('password'),
                'role' => 'super_admin',
            ]
        );

        $staffUser = User::updateOrCreate(
            ['email' => 'staff@humanitywelfare.org'],
            [
                'name' => 'Field Coordinator Staff',
                'password' => Hash::make('password'),
                'role' => 'staff',
            ]
        );

        // 2. Seed Colleges
        $collegesData = [
            ['name' => 'Delhi University (DU)', 'city_state' => 'New Delhi, Delhi'],
            ['name' => 'Jamia Millia Islamia (JMI)', 'city_state' => 'New Delhi, Delhi'],
            ['name' => 'Aligarh Muslim University (AMU)', 'city_state' => 'Aligarh, Uttar Pradesh'],
            ['name' => 'Indian Institute of Technology (IIT Delhi)', 'city_state' => 'New Delhi, Delhi'],
            ['name' => 'Amity University', 'city_state' => 'Noida, Uttar Pradesh'],
        ];

        $colleges = [];
        foreach ($collegesData as $c) {
            $colleges[] = College::create($c);
        }

        // 3. Seed Courses / Internship Domains
        $coursesData = [
            ['title' => 'Web Development & UI/UX', 'code' => 'WEB-101', 'description' => 'Full-stack web application development and UI design.'],
            ['title' => 'Social Work & Community Service', 'code' => 'SOC-201', 'description' => 'Field research and NGO community outreach.'],
            ['title' => 'Digital Marketing & Social Media', 'code' => 'MKT-301', 'description' => 'Content strategy and social media management.'],
            ['title' => 'Python & Data Analytics', 'code' => 'PY-401', 'description' => 'Data visualization and Python programming.'],
            ['title' => 'Graphic Design & Media Production', 'code' => 'DES-501', 'description' => 'Poster design, branding, and video editing.'],
        ];

        $courses = [];
        foreach ($coursesData as $crs) {
            $courses[] = Course::create($crs);
        }

        // 4. Seed Certificate Templates
        $t1 = CertificateTemplate::create([
            'title' => 'Certificate of Internship',
            'slug' => 'internship-certificate',
            'view_name' => 'pdf.templates.internship',
            'description' => 'Standard official internship completion certificate for students.',
        ]);

        $t2 = CertificateTemplate::create([
            'title' => 'Certificate of Appreciation',
            'slug' => 'appreciation-certificate',
            'view_name' => 'pdf.templates.appreciation',
            'description' => 'Issued to volunteers and interns for outstanding social service contribution.',
        ]);

        $t3 = CertificateTemplate::create([
            'title' => 'Certificate of Excellence',
            'slug' => 'excellence-certificate',
            'view_name' => 'pdf.templates.excellence',
            'description' => 'Special recognition certificate for top performing interns.',
        ]);

        // 5. Seed Sample Students & Certificates
        $sampleStudents = [
            [
                'full_name' => 'Aarav Sharma',
                'email' => 'aarav.sharma@example.com',
                'phone' => '+91 9876543210',
                'age' => 21,
                'dob' => '2005-04-15',
                'gender' => 'Male',
                'college_id' => $colleges[0]->id,
            ],
            [
                'full_name' => 'Fatima Khan',
                'email' => 'fatima.khan@example.com',
                'phone' => '+91 9812345678',
                'age' => 22,
                'dob' => '2004-08-20',
                'gender' => 'Female',
                'college_id' => $colleges[1]->id,
            ],
            [
                'full_name' => 'Rohan Verma',
                'email' => 'rohan.verma@example.com',
                'phone' => '+91 9765432109',
                'age' => 20,
                'dob' => '2006-01-10',
                'gender' => 'Male',
                'college_id' => $colleges[2]->id,
            ],
        ];

        foreach ($sampleStudents as $idx => $sData) {
            $student = Student::create($sData);

            $uuid = 'HWS-2026-'.strtoupper(Str::random(6));

            Certificate::create([
                'certificate_uuid' => $uuid,
                'student_id' => $student->id,
                'template_id' => ($idx == 0) ? $t1->id : (($idx == 1) ? $t2->id : $t3->id),
                'course_id' => $courses[$idx]->id,
                'internship_mode' => 'Online',
                'internship_nature' => 'Professional Internship',
                'start_date' => '2026-06-01',
                'end_date' => '2026-08-31',
                'duration' => '3 Months',
                'hours_completed' => 120,
                'performance_grade' => 'Grade A+',
                'role_designation' => 'Student Intern',
                'issue_date' => '2026-09-01',
                'status' => 'active',
                'issued_by' => $superAdmin->id,
                'signatory_title' => 'President & Secretary',
                'email_sent' => true,
                'email_sent_at' => now(),
                'print_count' => 1,
            ]);

            AuditLog::create([
                'user_id' => $superAdmin->id,
                'action' => 'CERTIFICATE_GENERATED',
                'details' => 'Generated certificate '.$uuid.' for student '.$student->full_name,
                'ip_address' => '127.0.0.1',
            ]);
        }
    }
}
