<?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::table('products', function (Blueprint $table) {
            $table->string('category', 120)->nullable()->after('name');
        });

        Schema::table('sales', function (Blueprint $table) {
            $table->foreignId('client_id')->nullable()->after('product_reservation_id')->constrained()->nullOnDelete();
            $table->decimal('subtotal', 14, 2)->default(0)->after('client_id');
            $table->decimal('discount_amount', 14, 2)->default(0)->after('subtotal');
            $table->string('discount_approval_code', 64)->nullable()->after('discount_amount');
            $table->string('payment_method', 32)->nullable()->after('discount_approval_code');
        });
    }

    public function down(): void
    {
        Schema::table('sales', function (Blueprint $table) {
            $table->dropConstrainedForeignId('client_id');
            $table->dropColumn([
                'subtotal',
                'discount_amount',
                'discount_approval_code',
                'payment_method',
            ]);
        });

        Schema::table('products', function (Blueprint $table) {
            $table->dropColumn('category');
        });
    }
};
