<?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('calendar_integrations', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('user_id')->constrained()->cascadeOnDelete();

            $table->string('provider');          // google | outlook
            $table->string('provider_email')->nullable();
            $table->string('calendar_id')->nullable();
            $table->string('calendar_name')->nullable();

            $table->text('access_token');
            $table->text('refresh_token')->nullable();
            $table->timestamp('token_expires_at')->nullable();

            // read = nur lesen | write = nur schreiben | both = beidseitig
            $table->string('sync_mode')->default('read');
            $table->text('sync_token')->nullable();
            $table->timestamp('last_synced_at')->nullable();

            $table->string('watch_channel_id')->nullable();
            $table->string('watch_resource_id')->nullable();
            $table->timestamp('watch_expires_at')->nullable();

            $table->timestamps();

            $table->unique(['user_id', 'provider']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('calendar_integrations');
    }
};
