<?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('orders', function (Blueprint $table) {
            $table->id();


            $table->foreignId('user_id')->constrained('users'); // سفارش‌دهنده
            $table->foreignId('producer_id')->nullable()->constrained('producers');
            $table->foreignId('product_id')->nullable()->constrained('products');
            $table->foreignId('project_id')->nullable()->constrained('projects');
            
            $table->string('order_number')->unique();
            $table->enum('type', ['order', 'request'])->default('order');
            
            $table->integer('quantity');
            $table->string('unit')->nullable();
            $table->decimal('unit_price', 15, 2)->nullable();
            $table->decimal('total_price', 15, 2);
            
            $table->dateTime('order_date');
            $table->dateTime('delivery_date')->nullable();
            $table->enum('status', ['pending', 'confirmed', 'processing', 'shipped', 'delivered', 'cancelled', 'rejected'])->default('pending');
            
            $table->foreignId('province_id')->nullable()->constrained('provinces');
            $table->foreignId('county_id')->nullable()->constrained('counties');
            $table->text('delivery_address')->nullable();
            
            $table->text('notes')->nullable();
            $table->json('metadata')->nullable();

            $table->foreignId('created_by')->constrained('users');
            $table->foreignId('updated_by')->nullable()->constrained('users');
            $table->softDeletes();
            $table->timestamps();
        });
    }

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