<?php

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

return new class extends Migration
{
    public function up(): void
    {
        $gpSurgeriesParent = DB::table('admin_menu')
            ->where('title', 'GP Surgeries')
            ->where('sub_id', 0)
            ->first();

        if (!$gpSurgeriesParent) {
            return;
        }

        // Remove old DoctorSurgery module menu items
        DB::table('admin_menu')
            ->where('sub_id', $gpSurgeriesParent->id)
            ->where('path', 'LIKE', '%DoctorSurgery%')
            ->delete();

        // Ensure the GP Addresses item exists under this parent
        $gpAddressesExists = DB::table('admin_menu')
            ->where('sub_id', $gpSurgeriesParent->id)
            ->where('path', '/admin/gp-addresses')
            ->exists();

        if (!$gpAddressesExists) {
            DB::table('admin_menu')->insert([
                'sub_id' => $gpSurgeriesParent->id,
                'title' => 'View GP Addresses List',
                'path' => '/admin/gp-addresses',
                'activePath' => '/admin/gp-addresses',
                'constant' => '',
                'icon' => 'fa fa-stethoscope',
                'new_window' => 0,
                'order' => 0,
            ]);
        } else {
            // Make it the first item
            DB::table('admin_menu')
                ->where('sub_id', $gpSurgeriesParent->id)
                ->where('path', '/admin/gp-addresses')
                ->update(['order' => 0]);
        }
    }

    public function down(): void
    {
        $gpSurgeriesParent = DB::table('admin_menu')
            ->where('title', 'GP Surgeries')
            ->where('sub_id', 0)
            ->first();

        if (!$gpSurgeriesParent) {
            return;
        }

        // Remove the gp-addresses entry installed by up() so rollback leaves
        // a clean menu rather than mixed legacy + new items.
        DB::table('admin_menu')
            ->where('sub_id', $gpSurgeriesParent->id)
            ->where('path', '/admin/gp-addresses')
            ->delete();

        // Restore old DoctorSurgery menu items idempotently so reruns or
        // partial rollbacks cannot duplicate rows.
        DB::table('admin_menu')->updateOrInsert(
            [
                'sub_id' => $gpSurgeriesParent->id,
                'path' => '/modules/DoctorSurgery/admin/upload.php',
            ],
            [
                'title' => 'Upload new doctor list',
                'activePath' => '/modules/DoctorSurgery/admin/upload',
                'constant' => '',
                'icon' => 'fa fa-upload',
                'new_window' => 0,
                'order' => 0,
            ]
        );

        DB::table('admin_menu')->updateOrInsert(
            [
                'sub_id' => $gpSurgeriesParent->id,
                'path' => '/modules/DoctorSurgery/admin/index.php',
            ],
            [
                'title' => 'View uploaded doctor list',
                'activePath' => '/modules/DoctorSurgery/admin/index',
                'constant' => '',
                'icon' => 'fa fa-list',
                'new_window' => 0,
                'order' => 1,
            ]
        );
    }
};
