<?php

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

class AddAccountingSyncColumns extends Migration
{
    public function up()
    {
        // Add to POS orders
        if (Schema::hasTable('pos_orders')) {
            Schema::table('pos_orders', function (Blueprint $table) {
                if (!Schema::hasColumn('pos_orders', 'accounting_synced')) {
                    $table->boolean('accounting_synced')->default(false);
                }
                if (!Schema::hasColumn('pos_orders', 'accounting_sync_at')) {
                    $table->timestamp('accounting_sync_at')->nullable();
                }
            });
        }
        
        // Add to manufacturing orders
        if (Schema::hasTable('manufacturing_orders')) {
            Schema::table('manufacturing_orders', function (Blueprint $table) {
                if (!Schema::hasColumn('manufacturing_orders', 'accounting_synced')) {
                    $table->boolean('accounting_synced')->default(false);
                }
                if (!Schema::hasColumn('manufacturing_orders', 'accounting_sync_at')) {
                    $table->timestamp('accounting_sync_at')->nullable();
                }
                if (!Schema::hasColumn('manufacturing_orders', 'total_material_cost')) {
                    $table->decimal('total_material_cost', 15, 2)->default(0);
                }
                if (!Schema::hasColumn('manufacturing_orders', 'labor_cost')) {
                    $table->decimal('labor_cost', 15, 2)->default(0);
                }
                if (!Schema::hasColumn('manufacturing_orders', 'total_cost')) {
                    $table->decimal('total_cost', 15, 2)->default(0);
                }
            });
        }
        
        // Add to service requests
        if (Schema::hasTable('service_requests')) {
            Schema::table('service_requests', function (Blueprint $table) {
                if (!Schema::hasColumn('service_requests', 'accounting_synced')) {
                    $table->boolean('accounting_synced')->default(false);
                }
                if (!Schema::hasColumn('service_requests', 'accounting_sync_at')) {
                    $table->timestamp('accounting_sync_at')->nullable();
                }
                if (!Schema::hasColumn('service_requests', 'billing_status')) {
                    $table->string('billing_status')->default('pending');
                }
                if (!Schema::hasColumn('service_requests', 'completed_at')) {
                    $table->timestamp('completed_at')->nullable();
                }
            });
        }
        
        // Add to reservations
        if (Schema::hasTable('reservations')) {
            Schema::table('reservations', function (Blueprint $table) {
                if (!Schema::hasColumn('reservations', 'accounting_synced')) {
                    $table->boolean('accounting_synced')->default(false);
                }
                if (!Schema::hasColumn('reservations', 'accounting_sync_at')) {
                    $table->timestamp('accounting_sync_at')->nullable();
                }
            });
        }
    }
    
    public function down()
    {
        // Remove columns if rolling back
        if (Schema::hasTable('pos_orders')) {
            Schema::table('pos_orders', function (Blueprint $table) {
                $table->dropColumn(['accounting_synced', 'accounting_sync_at']);
            });
        }
        
        if (Schema::hasTable('manufacturing_orders')) {
            Schema::table('manufacturing_orders', function (Blueprint $table) {
                $table->dropColumn(['accounting_synced', 'accounting_sync_at', 'total_material_cost', 'labor_cost', 'total_cost']);
            });
        }
        
        if (Schema::hasTable('service_requests')) {
            Schema::table('service_requests', function (Blueprint $table) {
                $table->dropColumn(['accounting_synced', 'accounting_sync_at', 'billing_status', 'completed_at']);
            });
        }
        
        if (Schema::hasTable('reservations')) {
            Schema::table('reservations', function (Blueprint $table) {
                $table->dropColumn(['accounting_synced', 'accounting_sync_at']);
            });
        }
    }
}