<?php

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

return new class extends Migration
{
    public function up(): void
    {
        DB::table('leads')->update([
            'stage_id' => DB::raw(
                "CASE
                    WHEN stage_id = 1 THEN 3
                    WHEN stage_id = 2 THEN 2
                    WHEN stage_id = 3 THEN 4
                    WHEN stage_id BETWEEN 4 AND 11 THEN 6
                    WHEN stage_id = 12 THEN 7
                    ELSE 1
                END"
            ),
        ]);

        $statuses = [
            1 => 'New Lead',
            2 => 'Need Analysis',
            3 => 'Attempted to Contact',
            4 => 'Introductory Meeting',
            5 => 'Contacted',
            6 => 'Qualified',
            7 => 'Unqualified',
        ];

        foreach ($statuses as $id => $name) {
            DB::table('lead_stages')->where('id', $id)->update([
                'name' => $name,
                'updated_at' => now(),
            ]);
        }
    }

    public function down(): void
    {
        $stages = [
            1 => 'Attempted to Contact',
            2 => 'Needs Analysis',
            3 => 'Introductory Meeting',
            4 => 'Proposal',
            5 => 'Quotation',
            6 => 'Presentation',
            7 => 'Demo',
        ];

        foreach ($stages as $id => $name) {
            DB::table('lead_stages')->where('id', $id)->update([
                'name' => $name,
                'updated_at' => now(),
            ]);
        }
    }
};
