<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $subjects = \App\Models\Subject::pluck('id', 'name');
        $grades   = \App\Models\Grade::pluck('id', 'level');

        // Admin
        \App\Models\User::create([
            'name'     => 'Admin',
            'email'    => 'admin@smart.lk',
            'password' => bcrypt('password'),
            'role'     => 'admin',
        ]);

        // Teachers – real data from the design
        $teachers = [
            ['name' => 'සකුණී ටීවර',    'name_en' => 'Sakuni Teacher',     'subject' => 'Dance',       'photo' => '662411703_n-e316529d.jpg', 'creds' => "University of Visual & Performing Arts\nBachelor of Performing Arts – Dance & Drama (Special)", 'exp' => 15],
            ['name' => 'ඩිලාන් සර',      'name_en' => 'Dilan Sir',          'subject' => 'Music',       'photo' => '656974909_n-07d3c704.jpg', 'creds' => "Western Music Grade 05\nLondon Royal College of Music", 'exp' => 10],
            ['name' => 'රුවන් සර',       'name_en' => 'Ruwan Sir',          'subject' => 'Maths',       'photo' => '659038114_n-0dd84a23.jpg', 'creds' => "BSc. Management (Special) – English Medium\nSabaragamuwa University of Sri Lanka", 'exp' => 18],
            ['name' => 'කළණී ටීවර',      'name_en' => 'Kalani Teacher',     'subject' => 'History',     'photo' => '657848696_n-9654a0a1.jpg', 'creds' => "BA History (Special)\nRajarata University of Sri Lanka", 'exp' => 15],
            ['name' => 'නුවන් සර',       'name_en' => 'Nuwan Sir',          'subject' => 'Science',     'photo' => '659212212_n-43a41e4c.jpg', 'creds' => "BSc (Hons) in Biological Sciences\nRajarata University of Sri Lanka", 'exp' => 10],
            ['name' => 'ශ්‍යාමලී ටීවර', 'name_en' => 'Shyamali Teacher',   'subject' => 'Scholarship', 'photo' => '654863622_n-59da2b94.jpg', 'creds' => "BA (sp) University of Peradeniya\nPost Graduate Diploma NIA (Primary Training)", 'exp' => 11],
            ['name' => 'කමලදාසන් සර',   'name_en' => 'Kamaladasan Sir',    'subject' => 'Tamil',       'photo' => '663229069_n-0cd4f9c1.jpg', 'creds' => "BA and Tamil Diploma", 'exp' => 15],
            ['name' => 'ලක්ෂාණී ටීවර',  'name_en' => 'Lakshani Teacher',   'subject' => 'English',     'photo' => '662296478_n-d9c5d8e1.jpg', 'creds' => "HNDE / BEd in English (Reading)", 'exp' => 11],
            ['name' => 'සමත් සර',        'name_en' => 'Samath Sir',         'subject' => 'ICT',         'photo' => '660404675_n-a8ec2596.jpg', 'creds' => "BTECH Networking, Dip in Cisco\nDip in Cyber Security", 'exp' => 10],
            ['name' => 'සුදත් සර',       'name_en' => 'Sudath Sir',         'subject' => 'Sinhala',     'photo' => '655505446_n-6fa4c93b.jpg', 'creds' => "National Diploma in Teaching (S.N.C.O.E)", 'exp' => 15],
        ];

        foreach ($teachers as $i => $t) {
            $emailBase = strtolower(str_replace(' ', '.', $t['name_en']));
            $user = \App\Models\User::create([
                'name'     => $t['name_en'],
                'name_si'  => $t['name'],
                'email'    => "{$emailBase}@smart.lk",
                'password' => bcrypt('password'),
                'role'     => 'teacher',
                'photo'    => $t['photo'],
                'phone'    => '071 234 567' . ($i + 1),
            ]);

            $subjectId = $subjects[$t['subject']] ?? null;

            // Link subject → teacher
            if ($subjectId) {
                \App\Models\Subject::where('id', $subjectId)
                    ->update(['teacher_id' => $user->id]);
            }

            \App\Models\TeacherProfile::create([
                'user_id'          => $user->id,
                'subject_id'       => $subjectId,
                'credentials'      => $t['creds'],
                'experience_years' => $t['exp'],
            ]);
        }

        // Students – seed 8 demo students in Grade 8
        $grade8 = $grades[8];
        $students = [
            ['name' => 'Amal Perera',         'batch' => 'A', 'fee' => 'paid'],
            ['name' => 'Nimal Silva',          'batch' => 'A', 'fee' => 'paid'],
            ['name' => 'Dilani Rathnayake',    'batch' => 'B', 'fee' => 'pending'],
            ['name' => 'Kavindu Bandara',      'batch' => 'A', 'fee' => 'paid'],
            ['name' => 'Hasini Fernando',      'batch' => 'C', 'fee' => 'paid'],
            ['name' => 'Tharuka Jayawardena',  'batch' => 'A', 'fee' => 'overdue'],
            ['name' => 'Malisha Rodrigo',      'batch' => 'B', 'fee' => 'paid'],
            ['name' => 'Ruwani Senanayake',    'batch' => 'A', 'fee' => 'paid'],
        ];

        // Core subjects for Grade 8 students
        $grade8Subjects = ['Maths', 'Science', 'English', 'History', 'Sinhala', 'ICT'];

        foreach ($students as $idx => $s) {
            $num  = str_pad($idx + 1, 3, '0', STR_PAD_LEFT);
            $user = \App\Models\User::create([
                'name'     => $s['name'],
                'email'    => strtolower(str_replace(' ', '.', $s['name'])) . '@student.smart.lk',
                'password' => bcrypt('password'),
                'role'     => 'student',
            ]);

            \App\Models\StudentProfile::create([
                'user_id'       => $user->id,
                'grade_id'      => $grade8,
                'student_id_no' => "SA{$num}",
                'batch'         => $s['batch'],
                'fee_status'    => $s['fee'],
                'joined_at'     => '2026-01-10',
            ]);

            // Enroll in Grade 8 subjects
            foreach ($grade8Subjects as $subjName) {
                if (isset($subjects[$subjName])) {
                    \DB::table('student_subject')->insertOrIgnore([
                        'user_id'     => $user->id,
                        'subject_id'  => $subjects[$subjName],
                        'enrolled_at' => now(),
                    ]);
                }
            }
        }
    }
}
