<?php

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

/**
 * Tables for the second wave of pet-owner PWA features:
 *   - vc_pet_weights:       owner-logged weight history (chart on PetDetail)
 *   - vc_pet_symptoms:      owner-submitted symptom diary + photos for triage
 *   - portal_messages:      two-way clinic↔owner inbox
 *   - portal_telehealth:    async photo-consult requests
 *   - portal_notifications: server-side log of pushes sent (for the owner
 *                           to re-read missed notifications inside the app)
 *   - portal_reviews:       owner reviews of completed appointments
 *
 * All are namespaced and scoped by business_id so a single ERP install
 * can serve many clinics.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vc_pet_weights')) {
            Schema::create('vc_pet_weights', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('pet_id');
                $t->decimal('weight_kg', 6, 2);
                $t->dateTime('recorded_at');
                $t->enum('source', ['owner','clinic'])->default('owner');
                $t->unsignedBigInteger('recorded_by')->nullable();
                $t->string('notes', 191)->nullable();
                $t->timestamps();
                $t->index(['business_id','pet_id','recorded_at']);
            });
        }

        if (! Schema::hasTable('vc_pet_symptoms')) {
            Schema::create('vc_pet_symptoms', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('pet_id');
                $t->unsignedBigInteger('contact_id')->nullable();
                $t->dateTime('recorded_at');
                $t->text('body');
                $t->enum('severity', ['low','moderate','high','urgent'])->default('low');
                $t->json('photo_paths')->nullable();
                $t->unsignedBigInteger('reply_by')->nullable();
                $t->text('reply_body')->nullable();
                $t->dateTime('replied_at')->nullable();
                $t->enum('status', ['open','answered','closed'])->default('open');
                $t->timestamps();
                $t->index(['business_id','pet_id','recorded_at']);
                $t->index(['business_id','status']);
            });
        }

        if (! Schema::hasTable('portal_messages')) {
            Schema::create('portal_messages', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('owner_id');
                $t->enum('sender', ['owner','clinic']);
                $t->unsignedBigInteger('sender_user_id')->nullable(); // ERP user when sender=clinic
                $t->text('body');
                $t->json('attachments')->nullable();
                $t->unsignedBigInteger('reply_to_id')->nullable();
                $t->dateTime('read_at')->nullable();
                $t->timestamps();
                $t->index(['business_id','owner_id','created_at']);
                $t->index(['business_id','owner_id','read_at']);
            });
        }

        if (! Schema::hasTable('portal_telehealth_requests')) {
            Schema::create('portal_telehealth_requests', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('owner_id');
                $t->unsignedBigInteger('pet_id');
                $t->text('question');
                $t->json('photo_paths')->nullable();
                $t->enum('status', ['pending','answered','closed'])->default('pending');
                $t->unsignedBigInteger('replied_by')->nullable();
                $t->text('reply_body')->nullable();
                $t->dateTime('replied_at')->nullable();
                $t->timestamps();
                $t->index(['business_id','owner_id','created_at']);
                $t->index(['business_id','status']);
            });
        }

        if (! Schema::hasTable('portal_notifications')) {
            Schema::create('portal_notifications', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('owner_id');
                $t->string('category', 32);
                $t->string('title', 191);
                $t->string('body', 500);
                $t->string('url', 300)->nullable();
                $t->dateTime('read_at')->nullable();
                $t->dateTime('created_at')->index();
                $t->dateTime('updated_at')->nullable();
                $t->index(['business_id','owner_id','created_at']);
            });
        }

        if (! Schema::hasTable('portal_reviews')) {
            Schema::create('portal_reviews', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('owner_id');
                $t->unsignedBigInteger('appointment_id');
                $t->unsignedBigInteger('doctor_id')->nullable();
                $t->tinyInteger('rating'); // 1-5
                $t->text('comments')->nullable();
                $t->timestamps();
                $t->unique(['appointment_id','owner_id']);
                $t->index(['business_id','doctor_id']);
            });
        }
    }

    public function down(): void
    {
        foreach ([
            'portal_reviews','portal_notifications','portal_telehealth_requests',
            'portal_messages','vc_pet_symptoms','vc_pet_weights',
        ] as $t) {
            Schema::dropIfExists($t);
        }
    }
};
