<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class UpdateServicesTableIconToImage extends Migration
{
    public function up()
    {
        // 1. Add image_path column
        $fields = [
            'image_path' => [
                'type'       => 'VARCHAR',
                'constraint' => '255',
                'null'       => true,
                'after'      => 'features',
            ],
        ];
        $this->forge->addColumn('services', $fields);

        // 2. Map existing icons to default service image assets
        $db = \Config\Database::connect();
        if ($db->tableExists('services')) {
            $db->table('services')->where('icon', 'heart')->update(['image_path' => base_url('images/service-wedding.png')]);
            $db->table('services')->where('icon', 'star')->update(['image_path' => base_url('images/service-upanayanam.png')]);
            $db->table('services')->where('icon', 'utensils')->update(['image_path' => base_url('images/service-catering.png')]);
            $db->table('services')->where('icon', 'flame')->update(['image_path' => base_url('images/service-pooja.png')]);
            // Fallback for any other icons
            $db->table('services')->where('image_path', null)->update(['image_path' => base_url('images/service-wedding.png')]);
        }

        // 3. Drop icon column
        $this->forge->dropColumn('services', 'icon');
    }

    public function down()
    {
        // 1. Add icon column back
        $fields = [
            'icon' => [
                'type'       => 'VARCHAR',
                'constraint' => '50',
                'null'       => true,
                'after'      => 'features',
            ],
        ];
        $this->forge->addColumn('services', $fields);

        // 2. Drop image_path column
        $this->forge->dropColumn('services', 'image_path');
    }
}
