<?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
    {
        // Add token and Stripe fields to job_variants
        Schema::table('job_variants', function (Blueprint $table) {
            $table->string('accept_token', 64)->nullable()->unique()->after('internal_notes');
            $table->string('reject_token', 64)->nullable()->unique()->after('accept_token');
            $table->string('stripe_session_id')->nullable()->after('reject_token');
            $table->string('stripe_payment_intent_id')->nullable()->after('stripe_session_id');
            $table->string('stripe_payment_status')->nullable()->after('stripe_payment_intent_id');
        });

        // Create job_variant_items table
        Schema::create('job_variant_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('job_variant_id')->constrained('job_variants')->cascadeOnDelete();
            $table->foreignId('material_id')->nullable()->constrained('materials')->nullOnDelete();
            $table->foreignId('material_type_id')->nullable()->constrained('material_types')->nullOnDelete();
            $table->string('description');
            $table->string('item_code')->nullable();
            $table->string('unit')->nullable()->default('Each');
            $table->decimal('qty', 10, 2)->default(1);
            $table->decimal('wsale_price', 10, 2)->default(0);
            $table->decimal('retail_price', 10, 2)->default(0);
            $table->text('comments')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('job_variant_items');
        Schema::table('job_variants', function (Blueprint $table) {
            $table->dropColumn(['accept_token', 'reject_token', 'stripe_session_id', 'stripe_payment_intent_id', 'stripe_payment_status']);
        });
    }
};
