<?php

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

echo "=== Checking for duplicates ===\n\n";

// Check Actions table
echo "ACTIONS TABLE:\n";
echo "-------------\n";
$actionDuplicates = DB::table('actions')
    ->select('name', DB::raw('COUNT(*) as total_count'))
    ->groupBy('name')
    ->havingRaw('COUNT(*) > 1')
    ->get();

if ($actionDuplicates->count() > 0) {
    echo "Found duplicate actions:\n";
    foreach ($actionDuplicates as $dup) {
        echo "  - '{$dup->name}' appears {$dup->total_count} times\n";
        
        // Get all IDs for this name
        $records = DB::table('actions')
            ->where('name', $dup->name)
            ->orderBy('id')
            ->get(['id', 'name', 'created_at']);
        
        echo "    IDs: ";
        foreach ($records as $record) {
            echo $record->id . " ";
        }
        echo "\n";
    }
} else {
    echo "No duplicates found.\n";
}

echo "\n";

// Check Decisions table
echo "DECISIONS TABLE:\n";
echo "---------------\n";
$decisionDuplicates = DB::table('decisions')
    ->select('name', DB::raw('COUNT(*) as total_count'))
    ->groupBy('name')
    ->havingRaw('COUNT(*) > 1')
    ->get();

if ($decisionDuplicates->count() > 0) {
    echo "Found duplicate decisions:\n";
    foreach ($decisionDuplicates as $dup) {
        echo "  - '{$dup->name}' appears {$dup->total_count} times\n";
        
        // Get all IDs for this name
        $records = DB::table('decisions')
            ->where('name', $dup->name)
            ->orderBy('id')
            ->get(['id', 'name', 'created_at']);
        
        echo "    IDs: ";
        foreach ($records as $record) {
            echo $record->id . " ";
        }
        echo "\n";
    }
} else {
    echo "No duplicates found.\n";
}

echo "\n=== Removing duplicates ===\n\n";

// Remove duplicate actions (keep the first one)
if ($actionDuplicates->count() > 0) {
    foreach ($actionDuplicates as $dup) {
        $records = DB::table('actions')
            ->where('name', $dup->name)
            ->orderBy('id')
            ->get(['id']);
        
        // Keep the first, delete the rest
        $toDelete = $records->skip(1)->pluck('id')->toArray();
        
        if (count($toDelete) > 0) {
            DB::table('actions')->whereIn('id', $toDelete)->delete();
            echo "Deleted duplicate actions for '{$dup->name}': IDs " . implode(', ', $toDelete) . "\n";
        }
    }
}

// Remove duplicate decisions (keep the first one)
if ($decisionDuplicates->count() > 0) {
    foreach ($decisionDuplicates as $dup) {
        $records = DB::table('decisions')
            ->where('name', $dup->name)
            ->orderBy('id')
            ->get(['id']);
        
        // Keep the first, delete the rest
        $toDelete = $records->skip(1)->pluck('id')->toArray();
        
        if (count($toDelete) > 0) {
            DB::table('decisions')->whereIn('id', $toDelete)->delete();
            echo "Deleted duplicate decisions for '{$dup->name}': IDs " . implode(', ', $toDelete) . "\n";
        }
    }
}

echo "\n";

// Check and clean action_decision pivot table
echo "ACTION_DECISION PIVOT TABLE:\n";
echo "---------------------------\n";

// Find duplicates in pivot table
$pivotDuplicates = DB::table('action_decision')
    ->select('action_id', 'decision_id', DB::raw('COUNT(*) as total_count'))
    ->groupBy('action_id', 'decision_id')
    ->havingRaw('COUNT(*) > 1')
    ->get();

if ($pivotDuplicates->count() > 0) {
    echo "Found duplicate pivot entries:\n";
    foreach ($pivotDuplicates as $dup) {
        echo "  - action_id: {$dup->action_id}, decision_id: {$dup->decision_id} appears {$dup->total_count} times\n";
        
        // Get all IDs for this combination
        $records = DB::table('action_decision')
            ->where('action_id', $dup->action_id)
            ->where('decision_id', $dup->decision_id)
            ->orderBy('id')
            ->get(['id']);
        
        // Keep the first, delete the rest
        $toDelete = $records->skip(1)->pluck('id')->toArray();
        
        if (count($toDelete) > 0) {
            DB::table('action_decision')->whereIn('id', $toDelete)->delete();
            echo "    Deleted duplicate pivot entries: IDs " . implode(', ', $toDelete) . "\n";
        }
    }
} else {
    echo "No duplicates found.\n";
}

echo "\n=== Cleanup complete ===\n";
