<?php

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

/**
 * REST API support tables:
 *   - bms_api_tokens:  custom bearer tokens for the two PWA guards
 *                      (client = a Contact, employee = a User). Mirrors the
 *                      existing PortalApi token scheme (no Sanctum).
 *   - bms_client_otps: one-time codes for client verification (issued only at
 *                      the commitment moment — visit booking / portal access).
 *   - bms_office_visits: office-visit bookings produced by the client funnel.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('bms_api_tokens')) {
            Schema::create('bms_api_tokens', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->enum('guard', ['client', 'employee']);
                $t->unsignedInteger('owner_id');          // contact_id (client) or user_id (employee)
                $t->string('token', 80)->unique();
                $t->json('abilities')->nullable();
                $t->dateTime('last_used_at')->nullable();
                $t->dateTime('expires_at')->nullable();
                $t->timestamps();
                $t->index(['guard', 'owner_id']);
                $t->index('business_id');
            });
        }

        if (! Schema::hasTable('bms_client_otps')) {
            Schema::create('bms_client_otps', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('phone', 30);
                $t->string('code', 10);
                $t->dateTime('expires_at');
                $t->unsignedTinyInteger('attempts')->default(0);
                $t->dateTime('consumed_at')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'phone']);
            });
        }

        if (! Schema::hasTable('bms_office_visits')) {
            Schema::create('bms_office_visits', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('lead_id')->nullable();
                $t->unsignedInteger('contact_id')->nullable();
                $t->unsignedInteger('location_id')->nullable();   // ERP business_location (one visit per location)
                $t->unsignedBigInteger('wizard_session_id')->nullable();
                $t->date('visit_date');
                $t->time('visit_time')->nullable();
                $t->enum('status', ['requested', 'scheduled', 'done', 'cancelled'])->default('requested');
                $t->string('notes', 255)->nullable();
                $t->unsignedInteger('created_by')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'visit_date']);
                $t->index(['business_id', 'lead_id']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('bms_office_visits');
        Schema::dropIfExists('bms_client_otps');
        Schema::dropIfExists('bms_api_tokens');
    }
};
