<?php

namespace App\Database\Migrations;

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

class CreateEmailSettings extends Migration
{
    public function up()
    {
        // Define the schema for the 'email_settings' table
        $this->forge->addField([
            'smtp_uid' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'null'       => false,
            ],
            'smtp_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
                'null'       => false,
            ],
            'smtp_host' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => false,
            ],
            'smtp_user' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => false,
            ],
            'smtp_pass' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => false,
            ],
            'smtp_port' => [
                'type'       => 'INT',
                'constraint' => 11,
                'null'       => false,
            ],
            'smtp_crypto' => [
                'type'       => 'VARCHAR',
                'constraint' => 10,
                'default'    => 'tls',
                'null'       => false,
            ],
            'from_email' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => false,
            ],
            'from_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => false,
            ],
            'smtp_default' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 0,
                'null'       => false,
            ],
            'created_at' => [
                'type' => 'TIMESTAMP',
                'null' => false,
            ],
        ]);

        // Add a primary key
        $this->forge->addPrimaryKey('smtp_uid');

        // Set the table's charset and collation
        $this->forge->createTable('email_settings', true, [
            'ENGINE' => 'InnoDB',
            'DEFAULT CHARSET' => 'utf8mb4',
            'COLLATE' => 'utf8mb4_general_ci',
        ]);
    }

    public function down()
    {
        // Drop the table
        $this->forge->dropTable('email_settings');
    }
}
