<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    public function up(): void {
        Schema::create('meta_connections', function (Blueprint $t) {
            $t->id(); $t->string('meta_user_id')->nullable(); $t->string('meta_user_name')->nullable();
            $t->text('access_token'); $t->timestamp('token_expires_at')->nullable();
            $t->string('status')->default('connected'); $t->timestamp('last_synced_at')->nullable();
            $t->text('last_error')->nullable(); $t->timestamps();
        });
        Schema::create('meta_pages', function (Blueprint $t) {
            $t->id(); $t->foreignId('meta_connection_id')->constrained()->cascadeOnDelete();
            $t->string('page_id')->unique(); $t->string('name'); $t->string('category')->nullable();
            $t->text('access_token'); $t->boolean('subscribed')->default(false); $t->string('status')->default('available');
            $t->timestamp('last_synced_at')->nullable(); $t->text('last_error')->nullable(); $t->timestamps();
        });
        Schema::create('meta_webhook_events', function (Blueprint $t) {
            $t->id(); $t->string('event_type')->nullable(); $t->string('object_id')->nullable()->index();
            $t->json('payload'); $t->string('status',30)->default('received'); $t->timestamp('processed_at')->nullable();
            $t->text('error_message')->nullable(); $t->timestamps();
        });
    }
    public function down(): void { Schema::dropIfExists('meta_webhook_events'); Schema::dropIfExists('meta_pages'); Schema::dropIfExists('meta_connections'); }
};
