<?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('video_shares', function (Blueprint $table) {
            $table->id();
            $table->foreignId('video_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('token', 12)->unique();
            $table->timestamp('created_at')->useCurrent();
        });

        Schema::create('share_accesses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('share_id')->constrained('video_shares')->cascadeOnDelete();
            $table->string('device_id', 64);
            $table->string('ip_address', 45)->nullable();
            $table->string('country', 2)->nullable();
            $table->string('country_name')->nullable();
            $table->timestamp('accessed_at')->useCurrent();
            $table->unique(['share_id', 'device_id']);
        });
    }

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