<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('settings', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->text('value')->nullable();
            $table->string('group')->default('general');
            $table->string('type')->default('string'); // string, boolean, integer, json
            $table->string('label');
            $table->text('description')->nullable();
            $table->boolean('is_public')->default(false);
            $table->timestamps();
        });

        // Seed default social auth settings
        $this->seedDefaultSettings();
    }

    public function down(): void
    {
        Schema::dropIfExists('settings');
    }

    protected function seedDefaultSettings(): void
    {
        $settings = [
            // Google OAuth
            [
                'key' => 'google_client_id',
                'value' => '',
                'group' => 'social_auth',
                'type' => 'string',
                'label' => 'Google Client ID',
                'description' => 'OAuth 2.0 Client ID from Google Cloud Console',
            ],
            [
                'key' => 'google_client_secret',
                'value' => '',
                'group' => 'social_auth',
                'type' => 'string',
                'label' => 'Google Client Secret',
                'description' => 'OAuth 2.0 Client Secret from Google Cloud Console',
            ],
            [
                'key' => 'google_enabled',
                'value' => '0',
                'group' => 'social_auth',
                'type' => 'boolean',
                'label' => 'Enable Google Login',
                'description' => 'Allow users to login with Google',
            ],
            
            // Facebook OAuth
            [
                'key' => 'facebook_client_id',
                'value' => '',
                'group' => 'social_auth',
                'type' => 'string',
                'label' => 'Facebook App ID',
                'description' => 'App ID from Facebook Developers',
            ],
            [
                'key' => 'facebook_client_secret',
                'value' => '',
                'group' => 'social_auth',
                'type' => 'string',
                'label' => 'Facebook App Secret',
                'description' => 'App Secret from Facebook Developers',
            ],
            [
                'key' => 'facebook_enabled',
                'value' => '0',
                'group' => 'social_auth',
                'type' => 'boolean',
                'label' => 'Enable Facebook Login',
                'description' => 'Allow users to login with Facebook',
            ],
            
            // GitHub OAuth
            [
                'key' => 'github_client_id',
                'value' => '',
                'group' => 'social_auth',
                'type' => 'string',
                'label' => 'GitHub Client ID',
                'description' => 'OAuth App Client ID from GitHub Settings',
            ],
            [
                'key' => 'github_client_secret',
                'value' => '',
                'group' => 'social_auth',
                'type' => 'string',
                'label' => 'GitHub Client Secret',
                'description' => 'OAuth App Client Secret from GitHub Settings',
            ],
            [
                'key' => 'github_enabled',
                'value' => '0',
                'group' => 'social_auth',
                'type' => 'boolean',
                'label' => 'Enable GitHub Login',
                'description' => 'Allow users to login with GitHub',
            ],
        ];

        foreach ($settings as $setting) {
            \DB::table('settings')->insert(array_merge($setting, [
                'created_at' => now(),
                'updated_at' => now(),
            ]));
        }
    }
};
