<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * Migrate data from business_trip_locations to unified locations table
     */
    public function up(): void
    {
        // Only run if business_trip_locations table exists
        if (!Schema::hasTable('business_trip_locations')) {
            return;
        }

        // Get all business trip locations
        $businessTripLocations = DB::table('business_trip_locations')->get();

        foreach ($businessTripLocations as $btl) {
            // Check if location with same name and company already exists
            $exists = DB::table('locations')
                ->where('company_id', $btl->company_id)
                ->where('name', $btl->name)
                ->exists();

            if (!$exists) {
                DB::table('locations')->insert([
                    'company_id' => $btl->company_id,
                    'name' => $btl->name,
                    'code' => $btl->code,
                    'address' => $btl->address,
                    'city' => $btl->city,
                    'province' => $btl->province,
                    'latitude' => $btl->latitude,
                    'longitude' => $btl->longitude,
                    'radius' => $btl->radius ?? 500,
                    'category' => 'external', // Business trip locations are external
                    'type' => $btl->type ?? 'other',
                    'allow_daily_checkin' => false, // Not for daily checkin
                    'allow_business_trip' => true,  // For business trips
                    'is_active' => $btl->is_active ?? true,
                    'is_primary' => $btl->is_primary ?? false,
                    'sort_order' => $btl->sort_order ?? 0,
                    'created_at' => $btl->created_at ?? now(),
                    'updated_at' => now(),
                ]);
            } else {
                // Update existing location to allow business trip
                DB::table('locations')
                    ->where('company_id', $btl->company_id)
                    ->where('name', $btl->name)
                    ->update([
                        'allow_business_trip' => true,
                        'is_primary' => DB::raw('COALESCE(is_primary, ' . ($btl->is_primary ? 'true' : 'false') . ')'),
                    ]);
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Remove locations that were migrated (category = external, allow_business_trip = true)
        // Be careful: this will delete data
        // Only delete if allow_daily_checkin = false (purely business trip locations)
        DB::table('locations')
            ->where('category', 'external')
            ->where('allow_business_trip', true)
            ->where('allow_daily_checkin', false)
            ->delete();
    }
};
