<?php

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

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

        $statuses = [
            1 => 'New',
            2 => 'Proposal',
            3 => 'Quotation',
            4 => 'Presentation',
            5 => 'Demo',
            6 => 'Negotiation',
            7 => 'Review',
            8 => 'Awaiting Decision',
            9 => 'Closed Won',
            10 => 'Closed Lost',
        ];

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

    public function down(): void
    {
        $stages = [
            1 => 'Qualification',
            2 => 'Campaign Assesment',
            3 => 'Introductory Meeting',
            4 => 'Needs Analysis',
            5 => 'Following Up',
            6 => 'Proposal / Quotation',
            7 => 'Presentation /Demo',
            8 => 'Negotiation/Review',
            9 => 'Awaiting decision',
            10 => 'Closed Won',
        ];

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