<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

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

        DB::table('content_models')->updateOrInsert(
            ['alias' => 'article'],
            [
                'name' => '文章',
                'description' => '默认文章模型',
                'table_name' => 'content_article',
                'sort' => 1,
                'status' => 1,
                'is_default' => 1,
                'create_time' => $now,
                'update_time' => $now,
            ]
        );

        $fields = [
            [
                'model_id' => 1,
                'name' => '副标题',
                'field' => 'subtitle',
                'type' => 'varchar',
                'length' => 200,
                'decimal_precision' => null,
                'default_value' => null,
                'form_type' => 'text',
                'form_config' => json_encode(['placeholder' => '请输入副标题']),
                'sort' => 1,
                'status' => 1,
                'create_time' => $now,
                'update_time' => $now,
            ],
            [
                'model_id' => 1,
                'name' => 'SEO关键词',
                'field' => 'keywords',
                'type' => 'varchar',
                'length' => 200,
                'decimal_precision' => null,
                'default_value' => null,
                'form_type' => 'tags',
                'form_config' => json_encode(['placeholder' => '多个关键词用逗号分隔', 'tips' => '用于SEO优化']),
                'sort' => 2,
                'status' => 1,
                'create_time' => $now,
                'update_time' => $now,
            ],
        ];
        foreach ($fields as $field) {
            DB::table('content_model_fields')->updateOrInsert(
                ['model_id' => $field['model_id'], 'field' => $field['field']],
                $field
            );
        }

        DB::table('content_categories')->updateOrInsert(
            ['name' => '文章分类', 'model_id' => 1],
            [
                'parent_id' => 0,
                'icon' => null,
                'image' => null,
                'description' => '默认文章分类',
                'sort' => 1,
                'status' => 1,
                'create_time' => $now,
                'update_time' => $now,
            ]
        );
    }
}
