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

            $table->foreignId('producer_id')->nullable()->constrained('producers');
            $table->foreignId('user_id')->nullable()->constrained('users');
            
            $table->foreignId('category_id')->nullable()->constrained('categories');
            $table->foreignId('sub_category_id')->nullable()->constrained('categories');
            $table->foreignId('type_id')->nullable()->constrained('categories');
            $table->foreignId('breed_id')->nullable()->constrained('breeds');
            
            $table->string('name');
            $table->text('excerpt')->nullable();
            $table->longText('desc')->nullable();
            
            $table->decimal('weight', 10, 2)->nullable();
            $table->string('weight_unit')->nullable();
            $table->integer('age')->nullable();
            $table->string('color')->nullable();
            $table->enum('gender', ['male', 'female', 'both', 'unknown'])->nullable();
            $table->integer('count')->nullable();
            $table->string('unit')->nullable();
            
            // اطلاعات والدین (برای دام)
            $table->string('father_info')->nullable();
            $table->string('mother_info')->nullable();
            
            // قیمت
            $table->decimal('price', 15, 2)->nullable();
            $table->date('price_active_date')->nullable();
            $table->decimal('total_price', 15, 2)->nullable();
            $table->decimal('price_per_unit', 15, 2)->nullable();
            
            // موقعیت
            $table->foreignId('province_id')->nullable()->constrained('provinces');
            $table->foreignId('county_id')->nullable()->constrained('counties');
            $table->text('address')->nullable();
            $table->string('postal_code')->nullable();
            $table->decimal('latitude', 10, 8)->nullable();
            $table->decimal('longitude', 11, 8)->nullable();
            $table->string('gps_location')->nullable();
            
            // تماس
            $table->string('phone')->nullable();
            $table->string('cellphone')->nullable();
            $table->string('email')->nullable();
            
            // رسانه
            $table->string('image')->nullable();
            $table->json('gallery')->nullable();
            $table->json('video')->nullable();
            $table->json('gallery_videos')->nullable();
            $table->json('documents')->nullable();
            $table->json('certificates')->nullable();
            
            // وضعیت
            $table->enum('status', ['active', 'inactive', 'pending', 'sold', 'deleted', 'expired'])->default('active');
            $table->boolean('is_active')->default(true);
            $table->boolean('is_featured')->default(false);
            
            // آمار
            $table->integer('view_count')->default(0);
            $table->integer('click_count')->default(0);
            $table->integer('request_count')->default(0);
            $table->integer('order_count')->default(0);
            $table->integer('offer_count')->default(0);
            $table->integer('message_count')->default(0);
            $table->integer('comment_count')->default(0);
            $table->decimal('rating_average', 3, 2)->nullable();
            $table->integer('rating_count')->default(0);

            $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('products');
    }
};
