<?php

declare(strict_types=1);

use Hyperf\Database\Schema\Blueprint;
use Hypervel\Database\Migrations\Migration;
use Hypervel\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('organizations', function (Blueprint $table) {
            $table->id();
            $table->string('hashkey', 300)->unique();
            $table->string('name');
            $table->string('type', 50)->default('COOPERATIVE'); // COOPERATIVE, ASSOCIATION, COMPANY
            $table->text('address')->nullable();
            $table->boolean('is_active')->default(true);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');
            $table->timestamps();
        });

        Schema::create('farmer_profiles', function (Blueprint $table) {
            $table->id();
            $table->string('hashkey', 300)->unique();
            $table->unsignedBigInteger('user_id')->index();
            $table->unsignedBigInteger('organization_id')->nullable()->index();
            $table->string('farm_name')->nullable();
            $table->text('farm_location')->nullable();
            $table->json('main_crops')->nullable();
            $table->string('verification_status', 50)->default('UNVERIFIED'); // UNVERIFIED, PENDING, VERIFIED, REJECTED
            $table->json('certification_details')->nullable();
            $table->boolean('is_active')->default(true);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('organization_id')->references('id')->on('organizations')->onDelete('set null');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('farmer_profiles');
        Schema::dropIfExists('organizations');
    }
};
