<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\PageGroup;
use App\Models\CustomPage;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Cache;

class CustomPageSeeder extends Seeder
{
    public function run(): void
    {
        // 1. Create Groups
        $groupCompany = PageGroup::firstOrCreate(
            ['name' => 'Company Info'],
            ['sort_order' => 1, 'is_active' => true]
        );

        $groupSupport = PageGroup::firstOrCreate(
            ['name' => 'Customer Care'],
            ['sort_order' => 2, 'is_active' => true]
        );

        $groupLegal = PageGroup::firstOrCreate(
            ['name' => 'Legal & Policies'],
            ['sort_order' => 3, 'is_active' => true]
        );

        // 2. Define Content using Heredoc (Safer for HTML)
        $aboutContent = <<<HTML
<h2>Welcome to Our Marketplace</h2>
<p><strong>Ownstore shopping is now more easy.</strong></p>
<p>We are a premier global marketplace dedicated to connecting buyers and verified suppliers from all around the world.</p>
<h3>Why Choose Us?</h3>
<ul>
    <li>Verified Suppliers & Quality Assurance</li>
    <li>Secure Payment Gateways</li>
    <li>Comprehensive Buyer Protection</li>
    <li>24/7 Dedicated Support</li>
</ul>
HTML;

        $contactContent = <<<HTML
<h2>Get in Touch</h2>
<p>If you have any questions, our support team is here to help you.</p>
<p><strong>Email:</strong> support@ownstore.com</p>
<p><strong>Phone:</strong> +1 (800) 123-4567</p>
HTML;

        $shippingContent = <<<HTML
<h2>Shipping Information</h2>
<p>We partner with global logistics providers including DHL and FedEx.</p>
<p>Shipping times depend on the supplier's location and your chosen delivery method.</p>
HTML;

        $privacyContent = <<<HTML
<h2>Privacy Policy</h2>
<p>Your privacy is critically important to us. We do not sell your personal data to third parties.</p>
HTML;

        // 3. Define Pages Array
        $pages = [
            [
                'group' => $groupCompany->id,
                'title' => 'About Us',
                'bottom' => false,
                'order' => 1,
                'body' => $aboutContent
            ],
            [
                'group' => $groupCompany->id,
                'title' => 'Contact Us',
                'bottom' => false,
                'order' => 2,
                'body' => $contactContent
            ],
            [
                'group' => $groupSupport->id,
                'title' => 'Help Center & FAQ',
                'bottom' => false,
                'order' => 1,
                'body' => '<h3>Frequently Asked Questions</h3><p>Contact support for more details.</p>'
            ],
            [
                'group' => $groupSupport->id,
                'title' => 'Shipping & Delivery',
                'bottom' => false,
                'order' => 2,
                'body' => $shippingContent
            ],
            [
                'group' => $groupLegal->id,
                'title' => 'Privacy Policy',
                'bottom' => true,
                'order' => 1,
                'body' => $privacyContent
            ],
            [
                'group' => $groupLegal->id,
                'title' => 'Terms of Service',
                'bottom' => true,
                'order' => 2,
                'body' => '<h2>Terms of Service</h2><p>By using this platform, you agree to our terms.</p>'
            ],
        ];

        // 4. Insert Data
        foreach ($pages as $p) {
            CustomPage::updateOrCreate(
                ['slug' => Str::slug($p['title'])],
                [
                    'page_group_id' => $p['group'],
                    'title' => $p['title'],
                    'content' => $p['body'],
                    'is_bottom_link' => $p['bottom'],
                    'is_active' => true,
                    'sort_order' => $p['order'],
                ]
            );
        }

        Cache::forget('footer_data');
    }
}