<?php

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

class CreateClcEvaluationTables extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('clc_evaluation_questions')) {
            Schema::create('clc_evaluation_questions', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id')->nullable()->index(); // null = system default
                $table->enum('category', ['pet_owner', 'clinic'])->index();
                $table->text('question_text');
                $table->enum('response_type', ['rating', 'text', 'yes_no'])->default('rating');
                $table->boolean('is_required')->default(false);
                $table->unsignedSmallInteger('sort_order')->default(0);
                $table->enum('status', ['active', 'inactive'])->default('active');
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('clc_evaluations')) {
            Schema::create('clc_evaluations', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id')->index();
                $table->unsignedInteger('appointment_id')->index();
                $table->enum('category', ['pet_owner', 'clinic'])->index();
                $table->unsignedInteger('evaluator_user_id')->nullable()->index();
                $table->unsignedInteger('doctor_id')->nullable()->index();
                $table->unsignedInteger('patient_id')->nullable()->index();
                $table->unsignedInteger('contact_id')->nullable()->index();
                $table->decimal('avg_rating', 3, 2)->nullable();
                $table->boolean('flagged_for_review')->default(false)->index();
                $table->unsignedInteger('reviewed_by')->nullable();
                $table->timestamp('reviewed_at')->nullable();
                $table->text('review_notes')->nullable();
                $table->text('notes')->nullable();
                $table->timestamps();

                $table->unique(['appointment_id', 'category'], 'clc_eval_apt_cat_unique');
            });
        }

        if (!Schema::hasTable('clc_evaluation_responses')) {
            Schema::create('clc_evaluation_responses', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('evaluation_id')->index();
                $table->unsignedInteger('question_id')->nullable();
                $table->text('question_text_snapshot');
                $table->enum('response_type', ['rating', 'text', 'yes_no']);
                $table->unsignedTinyInteger('rating')->nullable();
                $table->text('text_answer')->nullable();
                $table->boolean('yes_no_answer')->nullable();
                $table->timestamps();
            });
        }

        // Permissions for evaluations
        $perms = [
            'clinic.evaluate_pet_owner',
            'clinic.evaluate_doctor',
            'clinic.manage_evaluation_questions',
            'clinic.review_evaluations',
        ];
        foreach ($perms as $p) {
            Permission::findOrCreate($p, 'web');
        }
    }

    public function down()
    {
        Schema::dropIfExists('clc_evaluation_responses');
        Schema::dropIfExists('clc_evaluations');
        Schema::dropIfExists('clc_evaluation_questions');

        $perms = [
            'clinic.evaluate_pet_owner',
            'clinic.evaluate_doctor',
            'clinic.manage_evaluation_questions',
            'clinic.review_evaluations',
        ];
        Permission::whereIn('name', $perms)->delete();
    }
}
