<?php

use App\Models\PlatformSetting;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        $this->set('security', 'enable_email_login', true, 'boolean');
        $this->set('security', 'enable_google_login_only', false, 'boolean');
        $this->set('email', 'mail_driver', 'smtp');
        $this->set('email', 'smtp_host', 'smtp-relay.brevo.com');
        $this->set('email', 'smtp_port', 587, 'integer');
        $this->set('email', 'smtp_username', 'apikey');
        $this->set('email', 'smtp_encryption', 'tls');
        $this->set('email', 'from_email', 'no-reply@remotematch.local', 'email');
        $this->set('email', 'from_name', 'RemoteMatch');
        $this->set('email', 'reply_to_email', 'support@remotematch.local', 'email');
        $this->set('email', 'admin_notification_email', 'admin@remotematch.local', 'email');
        $this->set('email', 'brevo_enabled', true, 'boolean');
        $this->set('email', 'brevo_api_url', 'https://api.brevo.com/v3/smtp/email', 'url');
        $this->set('email', 'password_reset_otp_subject', 'Your RemoteMatch password reset OTP');
        $this->set('email', 'password_reset_otp_expires_minutes', 10, 'integer');
    }

    public function down(): void
    {
        $this->set('security', 'enable_email_login', false, 'boolean');
        $this->set('security', 'enable_google_login_only', true, 'boolean');
    }

    private function set(string $group, string $key, mixed $value, string $type = 'string'): void
    {
        PlatformSetting::query()->updateOrCreate(
            ['group' => $group, 'key' => $key],
            [
                'value' => $value,
                'type' => $type,
                'is_encrypted' => false,
                'is_public' => false,
            ],
        );
    }
};
