<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('attachments', function (Blueprint $table) {
            $table->id();
            $table->uuid('key')->unique();
            $table->string('path');
            $table->string('filename');
            $table->string('type');
            $table->string('mime_type');
            $table->string('extension', 20)->nullable();
            $table->unsignedBigInteger('size')->default(0);
            $table->timestamps();

            $table->index('type');
            $table->index('mime_type');
        });

        Schema::table('tenants', function (Blueprint $table) {
            $table->foreign('header_logo_id')
                ->references('id')
                ->on('attachments')
                ->cascadeOnDelete();

            $table->foreign('footer_logo_id')
                ->references('id')
                ->on('attachments')
                ->cascadeOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('tenants', function (Blueprint $table) {
            if (Schema::getConnection()->getDriverName() !== 'sqlite') {
                $table->dropForeign(['header_logo_id']);
                $table->dropForeign(['footer_logo_id']);
            }
        });

        Schema::dropIfExists('attachments');
    }
};
