<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\SelfPaced\Course;
use App\Models\SelfPaced\CourseCategory;
use App\Models\User;
use Illuminate\Support\Str;

/**
 * SelfPacedCourseSeeder
 *
 * Seeds 3 sample self-paced courses with realistic data, fees, and durations.
 * Courses are linked to categories and the Trainer user as instructor.
 *
 * Requirements: 15.2
 */
class SelfPacedCourseSeeder extends Seeder
{
    public function run(): void
    {
        $trainer = User::whereHas('roles', fn ($q) => $q->where('name', 'Trainers'))->first();
        $admin   = User::whereHas('roles', fn ($q) => $q->where('name', 'Super Manager'))->first();

        $instructorId = $trainer?->id ?? $admin?->id ?? 1;
        $createdById  = $admin?->id ?? 1;

        $healthCat       = CourseCategory::where('slug', 'health')->first();
        $leadershipCat   = CourseCategory::where('slug', 'leadership')->first();
        $communicationCat = CourseCategory::where('slug', 'communication')->first();

        $courses = [
            [
                'title'                    => 'Community Health Promotion and Disease Prevention',
                'slug'                     => 'community-health-promotion-disease-prevention',
                'description'              => 'This comprehensive course equips health workers and community volunteers with the knowledge and skills needed to promote health and prevent common diseases in South Sudan communities. Participants will learn evidence-based strategies for community engagement, health education, and disease surveillance.',
                'objectives'               => "By the end of this course, participants will be able to:\n- Explain the principles of community health promotion\n- Identify major communicable and non-communicable diseases prevalent in South Sudan\n- Design and implement community health education sessions\n- Apply behaviour change communication techniques\n- Conduct basic disease surveillance and reporting\n- Mobilise community resources for health promotion activities",
                'prerequisites'            => 'Basic literacy in English. No prior health training required. Suitable for community health workers, volunteers, and health facility staff.',
                'featured_image'           => null,
                'category_id'             => $healthCat?->id,
                'instructor_id'           => $instructorId,
                'fee_amount'              => 50000.00,
                'currency'                => 'UGX',
                'duration_hours'          => 8,
                'passing_score'           => 70,
                'exam_attempts_allowed'   => 3,
                'exam_time_limit'         => 60,
                'exam_questions_per_attempt' => 10,
                'status'                  => 'published',
                'created_by'              => $createdById,
                'updated_by'              => $createdById,
            ],
            [
                'title'                    => 'Health Sector Leadership and Management',
                'slug'                     => 'health-sector-leadership-management',
                'description'              => 'This course develops leadership and management competencies for health professionals working in South Sudan\'s health sector. It covers strategic planning, team leadership, resource management, and governance principles tailored to the challenges of health service delivery in resource-limited settings.',
                'objectives'               => "By the end of this course, participants will be able to:\n- Apply core leadership principles in health sector contexts\n- Develop and implement strategic plans for health facilities\n- Manage human resources effectively in health teams\n- Apply financial management principles to health budgets\n- Navigate governance structures in the South Sudan health system\n- Lead change management initiatives in health organisations",
                'prerequisites'            => 'At least 2 years of experience working in the health sector. Basic computer literacy recommended.',
                'featured_image'           => null,
                'category_id'             => $leadershipCat?->id,
                'instructor_id'           => $instructorId,
                'fee_amount'              => 75000.00,
                'currency'                => 'UGX',
                'duration_hours'          => 12,
                'passing_score'           => 75,
                'exam_attempts_allowed'   => 3,
                'exam_time_limit'         => 90,
                'exam_questions_per_attempt' => 15,
                'status'                  => 'published',
                'created_by'              => $createdById,
                'updated_by'              => $createdById,
            ],
            [
                'title'                    => 'Health Communication and Community Engagement',
                'slug'                     => 'health-communication-community-engagement',
                'description'              => 'This course builds practical skills in health communication, community mobilisation, and behaviour change communication (BCC). Participants will learn how to design effective health messages, engage diverse communities, and use various communication channels to promote health behaviour change in South Sudan.',
                'objectives'               => "By the end of this course, participants will be able to:\n- Apply principles of effective health communication\n- Design targeted health messages for different audiences\n- Use interpersonal communication techniques for health promotion\n- Develop community engagement strategies\n- Utilise social media and digital tools for health communication\n- Evaluate the effectiveness of health communication campaigns",
                'prerequisites'            => 'Basic literacy in English. Experience in community work is an advantage but not required.',
                'featured_image'           => null,
                'category_id'             => $communicationCat?->id,
                'instructor_id'           => $instructorId,
                'fee_amount'              => 0.00,
                'currency'                => 'UGX',
                'duration_hours'          => 6,
                'passing_score'           => 65,
                'exam_attempts_allowed'   => 5,
                'exam_time_limit'         => 45,
                'exam_questions_per_attempt' => 10,
                'status'                  => 'published',
                'created_by'              => $createdById,
                'updated_by'              => $createdById,
            ],
        ];

        foreach ($courses as $data) {
            Course::firstOrCreate(
                ['slug' => $data['slug']],
                $data
            );
        }

        $this->command->info('SelfPacedCourseSeeder: 3 courses seeded.');
    }
}
