<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Spatie\Permission\Models\Permission;

/**
 * Voice-to-text transcription settings for VetAI.
 *
 *  use_separate_transcription = false → transcription reuses the main
 *      assistant provider/credentials (single-agent setup).
 *  use_separate_transcription = true  → the t_* columns are used instead,
 *      so a clinic can run e.g. Gemini for text help + Whisper-on-Groq for
 *      voice (separate-agent setup).
 *
 *  t_provider supports:
 *    webspeech              - browser-native, no API, no cost
 *    whisper_openai         - Whisper-1 via OpenAI
 *    whisper_groq           - whisper-large-v3-turbo via Groq (cheap, fast)
 *    whisper_openai_compat  - any OpenAI /v1/audio/transcriptions endpoint
 */
class AddTranscriptionToVaiSettings extends Migration
{
    public function up()
    {
        Schema::table('vai_settings', function (Blueprint $table) {
            if (!Schema::hasColumn('vai_settings', 'use_separate_transcription')) {
                $table->boolean('use_separate_transcription')->default(false)->after('monthly_token_budget');
                $table->string('t_provider', 30)->nullable()->after('use_separate_transcription');
                $table->string('t_base_url', 255)->nullable()->after('t_provider');
                $table->text('t_api_key')->nullable()->after('t_base_url');
                $table->string('t_model', 100)->nullable()->after('t_api_key');
                $table->string('t_language_hint', 10)->default('auto')->after('t_model');
                $table->boolean('auto_structure_soap')->default(true)->after('t_language_hint');
                $table->boolean('consent_warning')->default(true)->after('auto_structure_soap');
            }
        });

        Permission::findOrCreate('vet_ai.use_transcription', 'web');
    }

    public function down()
    {
        Schema::table('vai_settings', function (Blueprint $table) {
            $table->dropColumn([
                'use_separate_transcription', 't_provider', 't_base_url',
                't_api_key', 't_model', 't_language_hint',
                'auto_structure_soap', 'consent_warning',
            ]);
        });
        Permission::where('name', 'vet_ai.use_transcription')->delete();
    }
}
