<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateDnsRecordsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'domain_id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
            ],
            'type' => [
                'type'       => 'VARCHAR',
                'constraint' => '10',
            ],
            'name' => [
                'type'       => 'VARCHAR',
                'constraint' => '255',
                'comment'    => 'Name of the record, e.g. @ or www',
            ],
            'content' => [
                'type'       => 'TEXT',
            ],
            'ttl' => [
                'type'       => 'INT',
                'constraint' => 11,
                'default'    => 1,
            ],
            'priority' => [
                'type'       => 'INT',
                'constraint' => 11,
                'null'       => true,
            ],
            'cloudflare_record_id' => [
                'type'       => 'VARCHAR',
                'constraint' => '255',
                'null'       => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('domain_id', 'domains', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('dns_records');
    }

    public function down()
    {
        $this->forge->dropTable('dns_records');
    }
}
