<?php

namespace Database\Seeders;

use App\Models\LegalPage;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\File;

class LegalPagesSeeder extends Seeder
{
    public function run(): void
    {
        $now = Carbon::now();

        // Define the legal pages and their corresponding file patterns
        $pages = [
            'impressum' => [
                'title' => [
                    'de' => 'Impressum',
                    'en' => 'Legal Notice',
                ],
                'files' => [
                    'de' => 'docs/content/legal/impressum-de.md',
                    'en' => 'docs/content/legal/impressum-en.md',
                ],
            ],
            'datenschutz' => [
                'title' => [
                    'de' => 'Datenschutzerklärung',
                    'en' => 'Privacy Policy',
                ],
                'files' => [
                    'de' => 'docs/content/legal/datenschutz-de.md',
                    'en' => 'docs/content/legal/datenschutz-en.md',
                ],
            ],
            'agb' => [
                'title' => [
                    'de' => 'Allgemeine Geschäftsbedingungen',
                    'en' => 'Terms and Conditions',
                ],
                'files' => [
                    'de' => 'docs/content/legal/agb-de.md',
                    'en' => 'docs/content/legal/agb-en.md',
                ],
            ],
            'widerruf' => [
                'title' => [
                    'de' => 'Widerrufsbelehrung',
                    'en' => 'Rights of withdrawal',
                ],
                'files' => [
                    'de' => 'docs/content/legal/widerrufsbelehrung-de.md',
                    'en' => 'docs/content/legal/widerrufsbelehrung-en.md',
                ],
            ],
        ];

        foreach ($pages as $slug => $config) {
            $bodyByLocale = [];

            foreach ($config['files'] as $locale => $filePath) {
                if (File::exists(base_path($filePath))) {
                    $bodyByLocale[$locale] = File::get(base_path($filePath));
                } else {
                    // Fallback to empty string if file doesn't exist
                    $bodyByLocale[$locale] = '';
                }
            }

            $this->upsert($slug, $config['title'], $bodyByLocale, $now);
        }
    }

    private function upsert(string $slug, array $titleByLocale, array $bodyByLocale, \DateTimeInterface $effectiveFrom): void
    {
        $existing = LegalPage::where('slug', $slug)->orderByDesc('version')->first();
        $version = $existing ? ($existing->version + 1) : 1;
        LegalPage::updateOrCreate(
            ['slug' => $slug, 'version' => $version],
            [
                'title' => $titleByLocale,
                'body_markdown' => $bodyByLocale,
                'locale_fallback' => 'de',
                'version' => $version,
                'effective_from' => $effectiveFrom,
                'is_published' => true,
            ]
        );
    }
}

