<?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::table('media_items', function (Blueprint $table) {
            if (! Schema::hasColumn('media_items', 'location_name')) {
                $table->string('location_name')->nullable()->after('gps_lng');
            }
            if (! Schema::hasColumn('media_items', 'is_draft')) {
                $table->boolean('is_draft')->default(false)->after('status');
            }
            if (! Schema::hasColumn('media_items', 'document_category')) {
                $table->string('document_category')->nullable()->after('contract_number');
            }
        });

        if (! Schema::hasTable('media_flags')) {
            Schema::create('media_flags', function (Blueprint $table) {
                $table->id();
                $table->foreignId('media_item_id')->constrained('media_items')->cascadeOnDelete();
                $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
                $table->text('reason');
                $table->timestamps();

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

        if (! Schema::hasTable('user_devices')) {
            Schema::create('user_devices', function (Blueprint $table) {
                $table->id();
                $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
                $table->string('name')->nullable();
                $table->string('device_type', 50)->nullable();
                $table->string('platform', 50)->nullable();
                $table->string('browser', 100)->nullable();
                $table->string('user_agent', 500)->nullable();
                $table->string('ip', 45)->nullable();
                $table->string('fingerprint', 191)->nullable();
                $table->boolean('is_current')->default(false);
                $table->boolean('is_blocked')->default(false);
                $table->timestamp('last_used_at')->nullable();
                $table->timestamps();

                $table->index(['user_id', 'is_blocked']);
            });
        }

        Schema::table('users', function (Blueprint $table) {
            if (! Schema::hasColumn('users', 'two_factor_channel')) {
                $table->string('two_factor_channel', 20)->nullable();
            }
            if (! Schema::hasColumn('users', 'two_factor_enabled_for_login')) {
                $table->boolean('two_factor_enabled_for_login')->default(false);
            }
            if (! Schema::hasColumn('users', 'two_factor_enabled_for_withdrawal')) {
                $table->boolean('two_factor_enabled_for_withdrawal')->default(false);
            }
            if (! Schema::hasColumn('users', 'two_factor_enabled_for_project_activation')) {
                $table->boolean('two_factor_enabled_for_project_activation')->default(false);
            }
        });

        if (! Schema::hasTable('market_prices')) {
            Schema::create('market_prices', function (Blueprint $table) {
                $table->id();
                $table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
                $table->foreignId('breed_id')->nullable()->constrained('breeds')->nullOnDelete();
                $table->foreignId('province_id')->nullable()->constrained('provinces')->nullOnDelete();
                $table->foreignId('county_id')->nullable()->constrained('counties')->nullOnDelete();
                $table->string('label');
                $table->unsignedBigInteger('price')->default(0);
                $table->decimal('change', 8, 2)->default(0);
                $table->timestamp('recorded_at')->nullable();
                $table->timestamps();

                $table->index(['category_id', 'breed_id', 'province_id', 'county_id']);
            });
        }

        Schema::table('producers', function (Blueprint $table) {
            if (! Schema::hasColumn('producers', 'bio')) {
                $table->text('bio')->nullable()->after('brand_name');
            }
        });
    }

    public function down(): void
    {
        Schema::table('producers', function (Blueprint $table) {
            $table->dropColumn('bio');
        });

        Schema::dropIfExists('market_prices');

        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn([
                'two_factor_channel',
                'two_factor_enabled_for_login',
                'two_factor_enabled_for_withdrawal',
                'two_factor_enabled_for_project_activation',
            ]);
        });

        Schema::dropIfExists('user_devices');
        Schema::dropIfExists('media_flags');

        Schema::table('media_items', function (Blueprint $table) {
            $table->dropColumn(['location_name', 'is_draft', 'document_category']);
        });
    }
};
