<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\SelfPaced\Course;
use App\Models\SelfPaced\CourseTopic;
use App\Models\SelfPaced\CourseSubTopic;
use App\Models\SelfPaced\CourseMedia;

/**
 * SelfPacedMediaSeeder
 *
 * Seeds sample media attachments (image, pdf, video) per subtopic.
 * Uses placeholder file paths — actual files are not required for seeding.
 * Depends on SelfPacedTopicSeeder having run first.
 *
 * Requirements: 15.3
 */
class SelfPacedMediaSeeder 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.subtopics')->get();

        foreach ($courses as $course) {
            foreach ($course->topics as $topicIndex => $topic) {
                foreach ($topic->subtopics as $subIndex => $subtopic) {
                    $this->seedMediaForSubtopic($subtopic, $topicIndex, $subIndex);
                }
            }
        }

        $this->command->info('SelfPacedMediaSeeder: Sample media attachments seeded.');
    }

    private function seedMediaForSubtopic(CourseSubTopic $subtopic, int $topicIndex, int $subIndex): void
    {
        // Alternate media types across subtopics for variety
        $mediaItems = $this->getMediaForSubtopic($subtopic->title, $topicIndex, $subIndex);

        foreach ($mediaItems as $order => $item) {
            CourseMedia::firstOrCreate(
                [
                    'subtopic_id' => $subtopic->id,
                    'file_name'   => $item['file_name'],
                ],
                [
                    'title'      => $item['title'],
                    'type'       => $item['type'],
                    'file_path'  => $item['file_path'],
                    'file_size'  => $item['file_size'],
                    'mime_type'  => $item['mime_type'],
                    'sort_order' => $order + 1,
                ]
            );
        }
    }

    private function getMediaForSubtopic(string $subtopicTitle, int $topicIndex, int $subIndex): array
    {
        // Cycle through different media types based on position
        $pattern = ($topicIndex + $subIndex) % 3;

        $slug = \Illuminate\Support\Str::slug($subtopicTitle, '_');
        $slug = substr($slug, 0, 40);

        if ($pattern === 0) {
            // Image + PDF
            return [
                [
                    'title'     => 'Illustrated Guide: ' . $subtopicTitle,
                    'type'      => 'image',
                    'file_path' => 'self-paced/media/images/' . $slug . '_guide.jpg',
                    'file_name' => $slug . '_guide.jpg',
                    'file_size' => 245760, // ~240 KB
                    'mime_type' => 'image/jpeg',
                ],
                [
                    'title'     => 'Reference Sheet: ' . $subtopicTitle,
                    'type'      => 'pdf',
                    'file_path' => 'self-paced/media/pdfs/' . $slug . '_reference.pdf',
                    'file_name' => $slug . '_reference.pdf',
                    'file_size' => 512000, // ~500 KB
                    'mime_type' => 'application/pdf',
                ],
            ];
        } elseif ($pattern === 1) {
            // Video + Image
            return [
                [
                    'title'     => 'Video Lecture: ' . $subtopicTitle,
                    'type'      => 'video',
                    'file_path' => 'self-paced/media/videos/' . $slug . '_lecture.mp4',
                    'file_name' => $slug . '_lecture.mp4',
                    'file_size' => 52428800, // ~50 MB
                    'mime_type' => 'video/mp4',
                ],
                [
                    'title'     => 'Infographic: ' . $subtopicTitle,
                    'type'      => 'image',
                    'file_path' => 'self-paced/media/images/' . $slug . '_infographic.png',
                    'file_name' => $slug . '_infographic.png',
                    'file_size' => 358400, // ~350 KB
                    'mime_type' => 'image/png',
                ],
            ];
        } else {
            // PDF + Presentation
            return [
                [
                    'title'     => 'Study Notes: ' . $subtopicTitle,
                    'type'      => 'pdf',
                    'file_path' => 'self-paced/media/pdfs/' . $slug . '_notes.pdf',
                    'file_name' => $slug . '_notes.pdf',
                    'file_size' => 819200, // ~800 KB
                    'mime_type' => 'application/pdf',
                ],
                [
                    'title'     => 'Presentation Slides: ' . $subtopicTitle,
                    'type'      => 'presentation',
                    'file_path' => 'self-paced/media/presentations/' . $slug . '_slides.pptx',
                    'file_name' => $slug . '_slides.pptx',
                    'file_size' => 2097152, // ~2 MB
                    'mime_type' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
                ],
            ];
        }
    }
}
