<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;
use CodeIgniter\Database\Config;

class InsertEmailSettings extends Migration
{
    public function up()
    {
        // Prepare an array of multiple email settings data
        $data = [
            [
                'smtp_name'    => 'HaMPo_SDC',
                'smtp_host'    => 'smtp.gmail.com',
                'smtp_user'    => 'hampo@sobkichu.com',
                'smtp_pass'    => 'lecjflsqvdlmbauv',
                'smtp_port'    => 587,
                'from_email'   => 'hampo@sobkichu.com',
                'from_name'    => 'HaMPo4',
                'smtp_default' => 0,
            ],
            [
                'smtp_name'    => 'Bricks_SDC',
                'smtp_host'    => 'smtp.gmail.com',
                'smtp_user'    => 'brick@sobkichu.com',
                'smtp_pass'    => 'sefwewotibyfyars',
                'smtp_port'    => 587,
                'from_email'   => 'brick@sobkichu.com',
                'from_name'    => 'Brick',
                'smtp_default' => 0,
            ],
            [
                'smtp_name'    => 'SBLHW',
                'smtp_host'    => '192.168.200.203',
                'smtp_user'    => 'sblhw',
                'smtp_pass'    => '@1zure#123',
                'smtp_port'    => 25,
                'from_email'   => 'sblhw@standardbankbd.com',
                'from_name'    => 'HaMPo',
                'smtp_default' => 1,
            ],
        ];

        // Loop through the data and set the values for smtp_uid and created_at
        foreach ($data as &$record) {
            $this->db->table('email_settings')
                ->set('smtp_uid', 'REPLACE(UUID(), "-", "")', false) // Generate the UUID using raw SQL
                ->set('created_at', 'CURRENT_TIMESTAMP', false)    // Set the current timestamp
                ->insert($record);
        }

    }

    public function down()
    {
        // Remove the inserted data
        $this->db->table('email_settings')->where('1', 1)->delete();
    }


    // private function forceLogout()
    // {
    //     // Get the database connection
    //     $db = \Config\Database::connect();

    //     // Delete all records from the custom session table (hampo_session)
    //     $db->table('hampo_session')->truncate(); // This will delete all active sessions

    //     // Optionally, if you're storing sessions in the default session cookie, you can clear it as well
    //     session()->destroy(); // Destroy the current session

    //     // Redirect the user to the login page after session destruction (optional)
    //     // return redirect()->to('/login');
    // }
    
}
