<?php

use App\Enums\CuttingStatus;
use App\Enums\PriceType;
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('cuttings', function (Blueprint $table) {
            $table->id();

            $table->foreignId('created_by_id')->constrained('users')->restrictOnDelete();
            $table->foreignId('submitted_by_id')->nullable()->constrained('users')->nullOnDelete();

            $table->enum('status', CuttingStatus::values())->default(CuttingStatus::IN_PROGRESS->value);
            $table->string('description', 100)->nullable();

            $table->unsignedBigInteger('total_material_cost')->nullable();
            $table->unsignedBigInteger('sewing_cost')->default(0);
            $table->unsignedBigInteger('other_cost')->default(0);
            $table->unsignedBigInteger('cost_per_unit')->nullable();

            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
            $table->softDeletes();

        });

        Schema::create('cutting_result_prices', function (Blueprint $table) {
            $table->id();

            $table->foreignId('cutting_id')->constrained()->cascadeOnDelete();
            $table->foreignId('product_variant_id')->constrained()->restrictOnDelete();

            $table->enum('price_type', PriceType::values());
            $table->unsignedBigInteger('price');
            $table->unsignedBigInteger('cost_per_unit');

            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();

            $table->unique(['cutting_id', 'product_variant_id', 'price_type'], 'cutting_result_prices_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('cutting_result_prices');
        Schema::dropIfExists('cuttings');
    }
};
