<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('purchase_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('purchase_id')
                ->constrained('purchases')
                ->cascadeOnDelete();
            $table->foreignId('purchase_product_id')
                ->constrained('purchase_products')
                ->restrictOnDelete();
            $table->decimal('quantity', 12, 3);
            $table->decimal('unit_price', 15, 2);
            $table->decimal('line_total', 15, 2);
            $table->timestamps();
        });

        if (Schema::hasColumn('purchases', 'purchase_product_id')) {
            $purchases = DB::table('purchases')->get();

            foreach ($purchases as $purchase) {
                if ($purchase->purchase_product_id === null) {
                    continue;
                }

                DB::table('purchase_items')->insert([
                    'purchase_id' => $purchase->id,
                    'purchase_product_id' => $purchase->purchase_product_id,
                    'quantity' => $purchase->quantity,
                    'unit_price' => $purchase->unit_price,
                    'line_total' => $purchase->total_amount,
                    'created_at' => $purchase->created_at,
                    'updated_at' => $purchase->updated_at,
                ]);
            }

            Schema::table('purchases', function (Blueprint $table) {
                $table->dropForeign(['purchase_product_id']);
                $table->dropColumn(['purchase_product_id', 'quantity', 'unit_price']);
            });
        }
    }

    public function down(): void
    {
        Schema::table('purchases', function (Blueprint $table) {
            $table->foreignId('purchase_product_id')
                ->nullable()
                ->after('created_by')
                ->constrained('purchase_products')
                ->restrictOnDelete();
            $table->decimal('quantity', 12, 3)->nullable()->after('bill_number');
            $table->decimal('unit_price', 15, 2)->nullable()->after('quantity');
        });

        $items = DB::table('purchase_items')
            ->orderBy('id')
            ->get()
            ->groupBy('purchase_id');

        foreach ($items as $purchaseId => $purchaseItems) {
            $first = $purchaseItems->first();

            DB::table('purchases')
                ->where('id', $purchaseId)
                ->update([
                    'purchase_product_id' => $first->purchase_product_id,
                    'quantity' => $first->quantity,
                    'unit_price' => $first->unit_price,
                    'total_amount' => $first->line_total,
                ]);
        }

        Schema::dropIfExists('purchase_items');
    }
};
