<?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
    {
        // Modify categories table to add parent_id, descriptions
        Schema::table('categories', function (Blueprint $table) {
            if (!Schema::hasColumn('categories', 'parent_id')) {
                $table->foreignId('parent_id')->nullable()->after('code')->constrained('categories')->onDelete('cascade');
            }
            if (!Schema::hasColumn('categories', 'description_en')) {
                $table->text('description_en')->nullable()->after('name_ar');
                $table->text('description_ar')->nullable()->after('description_en');
            }
        });

        // Modify brands table to add brand_code, description, logo
        Schema::table('brands', function (Blueprint $table) {
            if (!Schema::hasColumn('brands', 'code')) {
                $table->string('code')->nullable()->after('id');
            }
            if (!Schema::hasColumn('brands', 'description')) {
                $table->text('description')->nullable()->after('name_ar');
                $table->string('logo_path')->nullable()->after('description');
                $table->enum('status', ['active', 'inactive'])->default('active')->after('logo_path');
            }
        });

        // Modify units table to add short names and decimal allowed
        Schema::table('units', function (Blueprint $table) {
            if (!Schema::hasColumn('units', 'short_name_en')) {
                $table->string('short_name_en')->nullable()->after('name_ar');
                $table->string('short_name_ar')->nullable()->after('short_name_en');
                $table->boolean('allow_decimal')->default(true)->after('short_name_ar');
            }
        });

        // Create product_units table
        Schema::create('product_units', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained('products')->onDelete('cascade');
            $table->foreignId('unit_id')->constrained('units')->onDelete('cascade');
            $table->decimal('conversion_factor', 18, 4)->default(1.0000);
            $table->boolean('is_base_unit')->default(false);
            $table->boolean('is_purchase_unit')->default(false);
            $table->boolean('is_sales_unit')->default(false);
            $table->string('barcode')->nullable();
            $table->decimal('purchase_price', 18, 2)->default(0.00);
            $table->decimal('selling_price', 18, 2)->default(0.00);
            $table->decimal('wholesale_price', 18, 2)->default(0.00);
            $table->timestamps();

            $table->unique(['product_id', 'unit_id']);
        });

        // Create product_barcodes table
        Schema::create('product_barcodes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained('products')->onDelete('cascade');
            $table->foreignId('unit_id')->nullable()->constrained('units')->onDelete('cascade');
            $table->string('barcode')->unique();
            $table->string('barcode_type')->default('CODE128'); // EAN13, CODE128, UPC, etc.
            $table->boolean('is_primary')->default(false);
            $table->timestamps();
        });
    }

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