<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\SelfPaced\Course;
use App\Models\SelfPaced\CourseDownload;

/**
 * SelfPacedDownloadSeeder
 *
 * Seeds sample downloadable resources (PDF, Excel, Word, ZIP) per course and per topic.
 * Uses placeholder file paths — actual files are not required for seeding.
 * Depends on SelfPacedTopicSeeder having run first.
 *
 * Requirements: 15.3
 */
class SelfPacedDownloadSeeder extends Seeder
{
    public function run(): void
    {
        $courses = Course::whereIn('slug', [
            'community-health-promotion-disease-prevention',
            'health-sector-leadership-management',
            'health-communication-community-engagement',
        ])->with('topics')->get();

        foreach ($courses as $course) {
            $this->seedCourseDownloads($course);
            $this->seedTopicDownloads($course);
        }

        $this->command->info('SelfPacedDownloadSeeder: Sample downloadable resources seeded.');
    }

    private function seedCourseDownloads(Course $course): void
    {
        $slug = $course->slug;

        $downloads = $this->getCourseDownloads($course->title, $slug);

        foreach ($downloads as $item) {
            CourseDownload::firstOrCreate(
                ['course_id' => $course->id, 'file_name' => $item['file_name']],
                [
                    'topic_id'  => null,
                    'title'     => $item['title'],
                    'file_path' => $item['file_path'],
                    'file_size' => $item['file_size'],
                    'mime_type' => $item['mime_type'],
                ]
            );
        }
    }

    private function seedTopicDownloads(Course $course): void
    {
        foreach ($course->topics as $index => $topic) {
            $topicSlug = \Illuminate\Support\Str::slug($topic->title, '_');
            $topicSlug = substr($topicSlug, 0, 40);

            // Alternate between PDF and Excel per topic
            if ($index % 2 === 0) {
                CourseDownload::firstOrCreate(
                    ['topic_id' => $topic->id, 'file_name' => $topicSlug . '_worksheet.pdf'],
                    [
                        'course_id' => $course->id,
                        'title'     => 'Topic Worksheet: ' . $topic->title,
                        'file_path' => 'self-paced/downloads/topics/' . $topicSlug . '_worksheet.pdf',
                        'file_name' => $topicSlug . '_worksheet.pdf',
                        'file_size' => 307200, // ~300 KB
                        'mime_type' => 'application/pdf',
                    ]
                );
            } else {
                CourseDownload::firstOrCreate(
                    ['topic_id' => $topic->id, 'file_name' => $topicSlug . '_activity.xlsx'],
                    [
                        'course_id' => $course->id,
                        'title'     => 'Activity Tracker: ' . $topic->title,
                        'file_path' => 'self-paced/downloads/topics/' . $topicSlug . '_activity.xlsx',
                        'file_name' => $topicSlug . '_activity.xlsx',
                        'file_size' => 51200, // ~50 KB
                        'mime_type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                    ]
                );
            }
        }
    }

    private function getCourseDownloads(string $courseTitle, string $slug): array
    {
        return [
            [
                'title'     => 'Course Handbook: ' . $courseTitle,
                'file_path' => 'self-paced/downloads/courses/' . $slug . '_handbook.pdf',
                'file_name' => $slug . '_handbook.pdf',
                'file_size' => 2097152, // ~2 MB
                'mime_type' => 'application/pdf',
            ],
            [
                'title'     => 'Course Summary Notes: ' . $courseTitle,
                'file_path' => 'self-paced/downloads/courses/' . $slug . '_summary.pdf',
                'file_name' => $slug . '_summary.pdf',
                'file_size' => 614400, // ~600 KB
                'mime_type' => 'application/pdf',
            ],
            [
                'title'     => 'Self-Assessment Checklist: ' . $courseTitle,
                'file_path' => 'self-paced/downloads/courses/' . $slug . '_checklist.docx',
                'file_name' => $slug . '_checklist.docx',
                'file_size' => 102400, // ~100 KB
                'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            ],
            [
                'title'     => 'Resource Pack: ' . $courseTitle,
                'file_path' => 'self-paced/downloads/courses/' . $slug . '_resources.zip',
                'file_name' => $slug . '_resources.zip',
                'file_size' => 5242880, // ~5 MB
                'mime_type' => 'application/zip',
            ],
        ];
    }
}
