<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use AleshaCMS\Models\User;
use AleshaCMS\Models\CMS\Setting;
use AleshaCMS\Models\CMS\ContentType;
use Illuminate\Support\Facades\Hash;

class CMSSeeder extends Seeder
{
    public function run(): void
    {
        // Roles
        $roles = ['admin', 'editor', 'author', 'moderator', 'developer', 'user'];
        foreach ($roles as $role) {
            Role::firstOrCreate(['name' => $role]);
        }

        // Admin user
        $admin = User::firstOrCreate(
            ['email' => 'admin@cms.local'],
            [
                'name' => 'Admin',
                'password' => Hash::make('password'),
            ]
        );
        $admin->syncRoles(['admin']);

        // Default settings
        $defaults = [
            ['key' => 'site_name', 'value' => config('app.name'), 'group' => 'general'],
            ['key' => 'site_tagline', 'value' => 'Powered by AleshaCMS', 'group' => 'general'],
            ['key' => 'active_theme', 'value' => 'default', 'group' => 'general'],
            ['key' => 'posts_per_page', 'value' => '10', 'group' => 'reading'],
        ];

        foreach ($defaults as $s) {
            Setting::firstOrCreate(['key' => $s['key']], $s);
        }

        // Default content types
        ContentType::firstOrCreate(['slug' => 'event'], [
            'name' => 'Event',
            'icon' => 'calendar',
            'has_archive' => true,
            'rewrite_slug' => 'events',
            'is_active' => true,
            'fields' => [
                ['key' => 'event_date', 'label' => 'Event Date', 'type' => 'date'],
                ['key' => 'location', 'label' => 'Location', 'type' => 'text'],
                ['key' => 'organizer', 'label' => 'Organizer', 'type' => 'text'],
            ],
        ]);
    }
}
