<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreatePatientCore extends Migration {
  public function up() {
    $this->forge->addField([
      'InternalPID'   => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
      'PatientID'   => ['type' => 'VARCHAR', 'constraint' => 255],
      'AlternatePID'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Prefix'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'NameFirst'   => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'NameMiddle'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'NameMaiden'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'NameLast'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Suffix'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'NameAlias'   => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Sex'    => ['type' => 'VARCHAR', 'constraint' => 10, 'null' => true],
      'PlaceOfBirth'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Birthdate'   => ['type' => 'DATE', 'null' => true],
      'Street_1'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Street_2'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Street_3'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'City'      => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Province'    => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'ZIP'       => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'EmailAddress1' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'EmailAddress2' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Phone'     => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'MobilePhone'   => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Custodian'   => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'AccountNumber' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'Country'  => ['type' => 'VARCHAR', 'constraint' => 10, 'null' => true],
      'Race'    => ['type' => 'VARCHAR', 'constraint' => 10, 'null' => true],
      'MaritalStatus' => ['type' => 'VARCHAR', 'constraint' => 10, 'null' => true],
      'Religion'  => ['type' => 'VARCHAR', 'constraint' => 10, 'null' => true],
      'Ethnic'    => ['type' => 'VARCHAR', 'constraint' => 10, 'null' => true],
      'Citizenship'   => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'isDead'=> ['type' => 'VARCHAR', 'constraint' => 10, 'null' => true],
      'TimeOfDeath' => ['type' => 'DATETIME', 'null' => true],
      'LinkTo' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'CreateDate' => ['type' => 'DATETIME', 'null' => true],
      'DelDate' => ['type' => 'DATETIME', 'null' => true]
    ]);
    $this->forge->addKey('InternalPID', true);
    $this->forge->addUniqueKey('PatientID');
    $this->forge->addUniqueKey('AlternatePID');
    $this->forge->addUniqueKey('EmailAddress1');
    $this->forge->createTable('patient');

    $this->forge->addField([
      'PatAttID'   => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
      'InternalPID'=> ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'Address'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'UserID'   => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'CreateDate' => ['type' => 'DATETIME', 'null' => true],
      'DelDate'  => ['type' => 'DATETIME', 'null' => true],
    ]);
    $this->forge->addKey('PatAttID', true);
    $this->forge->addUniqueKey('Address');
    $this->forge->createTable('patatt');

    $this->forge->addField([
      'PatComID'   => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
      'InternalPID'=> ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'Comment'  => ['type' => 'TEXT', 'null' => true],
      'CreateDate' => ['type' => 'DATETIME', 'null' => true],
      'EndDate'  => ['type' => 'DATETIME', 'null' => true],
    ]);
    $this->forge->addKey('PatComID', true);
    $this->forge->addUniqueKey('InternalPID');
    $this->forge->createTable('patcom');

    $this->forge->addField([
      'PatIdtID'    => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
      'InternalPID'   => ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'IdentifierType'=> ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'Identifier'  => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
      'EffectiveDate' => ['type' => 'DATETIME', 'null' => true],
      'ExpirationDate'=> ['type' => 'DATETIME', 'null' => true],
      'CreateDate' => ['type' => 'DATETIME', 'null' => true],
      'DelDate'     => ['type' => 'DATETIME', 'null' => true],
    ]);
    $this->forge->addKey('PatIdtID', true);
    $this->forge->createTable('patidt');

    $this->forge->addField([
      'PatRelID'   => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
      'InternalPID'=> ['type' => 'INT', 'constraint' => 11, 'null' => true],
      'CreateDate' => ['type' => 'DATETIME', 'null' => true],
      'EndDate'  => ['type' => 'DATETIME', 'null' => true],
    ]);
    $this->forge->addKey('PatRelID', true);
    $this->forge->createTable('patrelation');

  }

  public function down() {
    $this->forge->dropTable('patrelation');
    $this->forge->dropTable('patidt');
    $this->forge->dropTable('patcom');
    $this->forge->dropTable('patatt');
    $this->forge->dropTable('patient');
  }
}
