<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use App\Models\Comment;
use App\Models\Content;
use App\Models\User;

class CommentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Clear existing records
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        Comment::truncate();
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        // Get some content to comment on
        $contents = Content::where('status', 'published')->take(3)->get();

        if ($contents->isEmpty()) {
            $this->command->warn('No published content found. Skipping comment seeder.');
            return;
        }

        // Get or create a test user
        $user = User::first();
        if (!$user) {
            $user = User::create([
                'name' => 'Test User',
                'email' => 'test@example.com',
                'password' => Hash::make('password'), // Fixed syntax error here
                'email_verified_at' => now(),
            ]);
        }

        $comments = [
            // Comment 0: Approved comment from registered user
            [
                'commentable_type' => Content::class,
                'commentable_id' => $contents[0]->id,
                'user_id' => $user->id,
                'guest_name' => null,
                'guest_email' => null,
                'content' => 'মাশাআল্লাহ! খুবই গুরুত্বপূর্ণ একটি বিষয় তুলে ধরেছেন। আমাদের দৈনন্দিন জীবনে এই শিক্ষাগুলো কাজে লাগানো উচিত।',
                'content_en' => 'MashaAllah! You have highlighted a very important topic. We should apply these teachings in our daily lives.',
                'content_ar' => 'ما شاء الله! لقد سلطت الضوء على موضوع مهم للغاية. يجب علينا تطبيق هذه التعاليم في حياتنا اليومية.',
                'likes' => 15,
                'dislikes' => 0,
                'status' => 'approved',
                'parent_id' => null,
                'ip_address' => '192.168.1.100',
                'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
            ],
            // Comment 1: Reply to first comment
            [
                'commentable_type' => Content::class,
                'commentable_id' => $contents[0]->id,
                'user_id' => null,
                'guest_name' => 'আব্দুল্লাহ',
                'guest_email' => 'abdullah@example.com',
                'content' => 'জাজাকাল্লাহ খাইরান। আপনার মন্তব্যের সাথে আমি একমত।',
                'content_en' => 'Jazakallah Khairan. I agree with your comment.',
                'content_ar' => 'جزاك الله خيراً. أوافق على تعليقك.',
                'likes' => 5,
                'dislikes' => 0,
                'status' => 'approved',
                'parent_id' => null, // Will update in loop
                'ip_address' => '192.168.1.101',
                'user_agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0)',
            ],
            // Comment 2: Pending comment from guest
            [
                'commentable_type' => Content::class,
                'commentable_id' => $contents[1]->id,
                'user_id' => null,
                'guest_name' => 'ফাতেমা',
                'guest_email' => 'fatema@example.com',
                'content' => 'আমার একটি প্রশ্ন আছে। এই বিষয়ে আরও বিস্তারিত জানতে চাই।',
                'content_en' => 'I have a question. I would like to know more details about this topic.',
                'content_ar' => 'لدي سؤال. أود معرفة المزيد من التفاصيل حول هذا الموضوع.',
                'likes' => 0,
                'dislikes' => 0,
                'status' => 'pending',
                'parent_id' => null,
                'ip_address' => '192.168.1.102',
                'user_agent' => 'Mozilla/5.0 (Linux; Android 11)',
            ],
            // Comment 3: Approved comment with likes
            [
                'commentable_type' => Content::class,
                'commentable_id' => $contents[2]->id,
                'user_id' => $user->id,
                'guest_name' => null,
                'guest_email' => null,
                'content' => 'এই আয়াতটি আমার খুব প্রিয়। প্রতিদিন পড়ি এবং অনুধাবনের চেষ্টা করি।',
                'content_en' => 'This verse is very dear to me. I read it daily and try to understand it.',
                'content_ar' => 'هذه الآية عزيزة جداً علي. أقرأها يومياً وأحاول فهمها.',
                'likes' => 25,
                'dislikes' => 1,
                'status' => 'approved',
                'parent_id' => null,
                'ip_address' => '192.168.1.103',
                'user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',
            ],
            // Comment 4: Another comment on first content
            [
                'commentable_type' => Content::class,
                'commentable_id' => $contents[0]->id,
                'user_id' => null,
                'guest_name' => 'মুহাম্মদ',
                'guest_email' => 'muhammad@example.com',
                'content' => 'আমাদের ওয়েবসাইটে আরও এমন শিক্ষামূলক কন্টেন্ট যোগ করার অনুরোধ করছি।',
                'content_en' => 'Requesting to add more educational content like this to our website.',
                'content_ar' => 'نطلب إضافة المزيد من المحتوى التعليمي مثل هذا إلى موقعنا.',
                'likes' => 8,
                'dislikes' => 0,
                'status' => 'approved',
                'parent_id' => null,
                'ip_address' => '192.168.1.104',
                'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0)',
            ],
        ];

        $insertedIds = [];

        foreach ($comments as $index => $commentData) {
            // Handle parent_id for reply (Index 1 is the reply to Index 0)
            $parentId = $commentData['parent_id'];
            if ($index === 1 && isset($insertedIds[0])) {
                $parentId = $insertedIds[0];
            }

            // Store content as JSON
            $encodedContent = json_encode([
                'bn' => $commentData['content'],
                'en' => $commentData['content_en'],
                'ar' => $commentData['content_ar'],
            ], JSON_UNESCAPED_UNICODE);

            $id = DB::table('comments')->insertGetId([
                'commentable_type' => $commentData['commentable_type'],
                'commentable_id' => $commentData['commentable_id'],
                'user_id' => $commentData['user_id'],
                'guest_name' => $commentData['guest_name'],
                'guest_email' => $commentData['guest_email'],
                'content' => $encodedContent,
                'likes' => $commentData['likes'],
                'dislikes' => $commentData['dislikes'],
                'status' => $commentData['status'],
                'parent_id' => $parentId,
                'ip_address' => $commentData['ip_address'],
                'user_agent' => $commentData['user_agent'],
                'created_at' => now()->subDays(rand(1, 30)),
                'updated_at' => now(),
            ]);

            $insertedIds[$index] = $id;
        }

        $this->command->info('Successfully seeded 5 comments!');
    }
}
