<?php

namespace Database\Seeders;

use App\Models\Post;
use App\Models\PostTranslation;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class PlaceholderPostsSeeder extends Seeder
{
    /**
     * Create placeholder posts for all SiteContents categories
     * These posts have only titles in all available languages for each category
     *
     * Usage:
     * php artisan db:seed --class=PlaceholderPostsSeeder
     */
    public function run(): void
    {
        // Get all categories where type starts with 'SiteContents\'
        $categories = DB::table('categories')
            ->get()
            ->filter(function ($category) {
                return str_starts_with($category->type, 'SiteContents\\');
            });

        $totalCategories = $categories->count();
        $processed = 0;
        $created = 0;
        $skipped = 0;

        $this->command->info("Found {$totalCategories} SiteContents categories");

        foreach ($categories as $category) {
            $processed++;

            // Check if a post already exists for this category
            $postExists = DB::table('posts')
                ->where('category_id', $category->id)
                ->exists();

            if ($postExists) {
                // Skip if post already exists for this category
                $skipped++;
                $this->command->info("[{$processed}/{$totalCategories}] Skipped {$category->type} (already exists)");
                continue;
            }

            // Get available locales for this category
            $categoryTranslations = DB::table('category_translations')
                ->where('category_id', $category->id)
                ->get();

            // Skip if no translations exist for this category
            if ($categoryTranslations->isEmpty()) {
                $skipped++;
                $this->command->warn("[{$processed}/{$totalCategories}] Skipped {$category->type} (no translations)");
                continue;
            }

            // Create the post using Eloquent without dispatching events
            $post = Post::withoutEvents(function () use ($category) {
                return Post::create([
                    'postable_id' => 1,
                    'postable_type' => 'App\\Models\\Admin',
                    'category_id' => $category->id,
                ]);
            });

            $translationCount = 0;

            // Create post translations for each available locale
            foreach ($categoryTranslations as $categoryTranslation) {
                $title = "Placeholder: {$categoryTranslation->name}";

                PostTranslation::create([
                    'post_id' => $post->id,
                    'locale' => $categoryTranslation->locale,
                    'title' => $title,
                    'excerpt' => null,
                    'content' => null,
                    'status' => 1,
                    'updated_by_user_id' => null,
                    'from' => null,
                    'till' => null,
                ]);

                $translationCount++;
            }

            $created++;
            $this->command->info("[{$processed}/{$totalCategories}] Created post for {$category->type} ({$translationCount} translations)");
        }

        $this->command->info("\n✓ Seeding completed!");
        $this->command->info("  Created: {$created} posts");
        $this->command->info("  Skipped: {$skipped} posts");
    }
}
