<?php

/*
 * *************************************************
 * Copyright (C) 2021 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 * $this->dbforge->add_field(static::id() + [
  ] + static::default());
 */

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description of MY_Migration
 *
 * @author Andhy Taslin <andhy@earworks.co.id>
 * @since 1.0
 */
class MY_Migration extends CI_Migration {

       /**
        * ID = BIGINT(20), unsigned, autoincrement
        * @return array Key = id
        */
       public static function id() {
              return [
                      'id' => [
                              'type' => 'BIGINT',
                              'constraint' => 20,
                              'unsigned' => true,
                              'auto_increment' => true
                      ]
              ];
       }

       /**
        * inherit from BIGINT
        * @param array $custom
        * @return array
        */
       public static function foreign($custom = []) {
              return array_merge([
                      'type' => 'BIGINT',
                      'constraint' => 20,
                      'unsigned' => true,
                      'null' => true,
              ], $custom);
       }

       /**
        * SMALL ID = Smallint(5), unsigned, autoincrement
        * @return array Key = id
        */
       public static function small_id() {
              return [
                      'id' => [
                              'type' => 'SMALLINT',
                              'constraint' => 5,
                              'unsigned' => true,
                              'auto_increment' => true
                      ]
              ];
       }

       /**
        * inherit from SMALLINT
        * @param array $custom
        * @return array
        */
       public static function foreign_small($custom = []) {
              return self::smallint($custom);
       }

       /**
        * KODE = Varchar(10), not null
        * @return array Key = kode
        */
       public static function kode() {
              return [
                      'kode' => [
                              'type' => 'VARCHAR',
                              'constraint' => 10,
                              'null' => false
                      ]
              ];
       }

       /**
        * FOREIGN KODE = Varchar(10), null
        * @param array $custom
        * @return array
        */
       public static function foreign_kode($custom = []) {
              return array_merge([
                      'type' => 'VARCHAR',
                      'constraint' => 10,
                      'null' => true
              ], $custom);
       }

       /**
        * TEXT = Varchar(100), null
        * @param type $custom
        * @return type
        */
       public static function text($custom = []) {
              return array_merge([
                      'type' => 'VARCHAR',
                      'constraint' => 100,
                      'null' => true
              ], $custom);
       }

       /**
        * LONGTEXT = Text, null
        * @param array $custom
        * @return array
        */
       public static function longtext($custom = []) {
              return array_merge([
                      'type' => 'TEXT',
                      'null' => true
              ], $custom);
       }

       /**
        * DECIMAL = Decimal(50,3), null, default = 0
        * @param array $custom
        * @return array
        */
       public static function decimal($custom = []) {
              return array_merge([
                      'type' => 'DECIMAL',
                      'constraint' => '50,3',
                      'default' => 0,
                      'null' => true
              ], $custom);
       }

       /**
        * DATE = timestamp, null
        * @param array $custom
        * @return array
        */
       public static function date($custom = []) {
              return array_merge([
                      'type' => 'timestamp',
                      'null' => true
              ], $custom);
       }

       /**
        * DATE = TIME, null
        * @param array $custom
        * @return array
        */
       public static function time($custom = []) {
              return array_merge([
                      'type' => 'TIME',
                      'null' => true
              ], $custom);
       }

       /**
        * OPTION = Smallint(1), null, default = 1, comment = [1 = ACTIVE, 2 = BANNED]
        * @param array $custom
        * @return array
        */
       public static function option($custom = []) {
              return array_merge([
                      'type' => 'SMALLINT',
                      'constraint' => 1,
                      'null' => true,
                      'default' => 1,
                      'comment' => '1 = ACTIVE, 2 = BANNED'
              ], $custom);
       }

       /**
        * ORDER = Smallint(10), null, default = 0
        * @param array $custom
        * @return array
        */
       public static function order($custom = []) {
              return array_merge([
                      'type' => 'SMALLINT',
                      'constraint' => 10,
                      'null' => true,
                      'default' => 0
              ], $custom);
       }

       /**
        * BIGINT = Bigint(20), unsigned, null
        * @param array $custom
        * @return array
        */
       public static function bigint($custom = []) {
              return array_merge([
                      'type' => 'BIGINT',
                      'constraint' => 20,
                      'unsigned' => true,
                      'null' => true,
                      'default' => 0
              ], $custom);
       }

       /**
        * INT = int(11), null, default = 0
        * @param array $custom
        * @return array
        */
       public static function int($custom = []) {
              return array_merge([
                      'type' => 'INT',
                      'constraint' => 11,
                      'null' => true,
                      'unsigned' => true,
                      'default' => 0
              ], $custom);
       }

       /**
        * SMALLINT = smallint(5), null, unsigned, default = 0
        * @param array $custom
        * @return array
        */
       public static function smallint($custom = []) {
              return array_merge([
                      'type' => 'SMALLINT',
                      'constraint' => 5,
                      'null' => true,
                      'unsigned' => true,
                      'default' => 0
              ], $custom);
       }

       /**
        * inherit from OPTION
        * @param string $comment
        * @return array
        */
       public function status($comment = '1 = ACTIVE, 2 = BANNED') {
              return self::option([
                      'comment' => $comment
              ]);
       }

       /**
        * Default for all table
        * @return array Key = active, created_at, updated_at, deleted_at, created_by, updated_by, deleted_by
        */
       public static function default() {
              return [
                      'active' => self::option([
                              'default' => 1,
                              'comment' => '0 = INACTIVE, 1 = ACTIVE'
                      ]),
                      'created_at' => self::date(),
                      'updated_at' => self::date(),
                      'deleted_at' => self::date(),
                      'created_by' => self::bigint(),
                      'updated_by' => self::bigint(),
                      'deleted_by' => self::bigint(),
              ];
       }

}
