<?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('categories', function (Blueprint $table) {
            $table->id();
            $table->string('code')->nullable();
            $table->string('name_en');
            $table->string('name_ar')->nullable();
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->timestamps();
        });

        Schema::create('units', function (Blueprint $table) {
            $table->id();
            $table->string('code')->nullable();
            $table->string('name_en');
            $table->string('name_ar')->nullable();
            $table->timestamps();
        });

        Schema::create('brands', function (Blueprint $table) {
            $table->id();
            $table->string('name_en');
            $table->string('name_ar')->nullable();
            $table->timestamps();
        });

        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('customer_code')->unique();
            $table->string('name_en');
            $table->string('name_ar')->nullable();
            $table->string('vat_number')->nullable();
            $table->string('cr_number')->nullable();
            $table->string('building_number')->nullable();
            $table->string('street_name_en')->nullable();
            $table->string('street_name_ar')->nullable();
            $table->string('district_en')->nullable();
            $table->string('district_ar')->nullable();
            $table->string('city_en')->nullable();
            $table->string('city_ar')->nullable();
            $table->string('postal_code')->nullable();
            $table->string('additional_number')->nullable();
            $table->string('country')->default('Saudi Arabia');
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->decimal('credit_limit', 18, 2)->default(0.00);
            $table->string('customer_type')->default('Credit');
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('suppliers', function (Blueprint $table) {
            $table->id();
            $table->string('supplier_code')->unique();
            $table->string('name_en');
            $table->string('name_ar')->nullable();
            $table->string('vat_number')->nullable();
            $table->string('cr_number')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('product_code')->unique();
            $table->string('barcode')->nullable()->index();
            $table->string('name_en');
            $table->string('name_ar')->nullable();
            $table->text('description_en')->nullable();
            $table->text('description_ar')->nullable();
            $table->foreignId('category_id')->nullable()->constrained('categories')->onDelete('set null');
            $table->foreignId('unit_id')->nullable()->constrained('units')->onDelete('set null');
            $table->foreignId('brand_id')->nullable()->constrained('brands')->onDelete('set null');
            $table->decimal('cost_price', 18, 4)->default(0.0000);
            $table->decimal('sale_price', 18, 2)->default(0.00);
            $table->decimal('min_sale_price', 18, 2)->default(0.00);
            $table->decimal('vat_rate', 5, 2)->default(15.00);
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->softDeletes();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('products');
        Schema::dropIfExists('suppliers');
        Schema::dropIfExists('customers');
        Schema::dropIfExists('brands');
        Schema::dropIfExists('units');
        Schema::dropIfExists('categories');
    }
};
