<?php
declare(strict_types=1);

use Migrations\AbstractMigration;
use Phinx\Db\Adapter\MysqlAdapter;

class MailingLists extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     * @return void
     */

    public $autoId = false; // turn off automatic `id` column create. We want it to be `int(10) unsigned`


    public function change()
    {
        $mailinglists = $this->table('mailing_lists', [
            'signed' => false,
            'collation' => 'utf8mb4_unicode_ci',
        ]);
        $mailinglists
            ->addColumn('id', 'integer', [
                'autoIncrement' => true,
                'limit' => 10,
                'signed' => false,
            ])
            ->addPrimaryKey('id')
            ->addColumn('uuid', 'uuid', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('name', 'string', [
                'default' => null,
                'null' => false,
                'limit' => 191,
                'comment' => 'The name of the mailing list',
            ])
            ->addColumn('recipients', 'string', [
                'default' => null,
                'null' => true,
                'limit' => 191,
                'comment' => 'Human-readable description of who the intended recipients.',
            ])
            ->addColumn('description', 'text', [
                'default' => null,
                'null' => true,
                'comment' => 'Additional description of the mailing list'
            ])
            ->addColumn('user_id', 'integer', [
                'default' => null,
                'null' => true,
                'signed' => false,
                'length' => 10,
            ])
            ->addColumn('active', 'boolean', [
                'default' => 0,
                'null' => false,
            ])
            ->addColumn('deleted', 'boolean', [
                'default' => 0,
                'null' => false,
            ])
            ->addColumn('created', 'datetime', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('modified', 'datetime', [
                'default' => null,
                'null' => false,
            ]);


        $mailinglists->addForeignKey('user_id', 'users', 'id', ['delete' => 'CASCADE', 'update' => 'CASCADE']);

        $mailinglists->addIndex(['uuid'], ['unique' => true])
              ->addIndex('name')
              ->addIndex('recipients')
              ->addIndex('user_id')
              ->addIndex('active')
              ->addIndex('deleted')
              ->addIndex('created')
              ->addIndex('modified');

        $mailinglists->create();


        $mailinglists_individuals = $this->table('mailing_lists_individuals', [
            'signed' => false,
            'collation' => 'utf8mb4_unicode_ci',
        ]);

        $mailinglists_individuals
            ->addColumn('id', 'integer', [
                'autoIncrement' => true,
                'limit' => 10,
                'signed' => false,
            ])
            ->addPrimaryKey('id')
            ->addColumn('mailing_list_id', 'integer', [
                'default' => null,
                'null' => true,
                'signed' => false,
                'length' => 10,
            ])
            ->addColumn('individual_id', 'integer', [
                'default' => null,
                'null' => true,
                'signed' => false,
                'length' => 10,
            ])
            ->addColumn('include_primary_email', 'boolean', [
                'default' => 1,
                'null' => false,
                'comment' => 'Should the primary email address by included in the mailing list'
            ])
            ->addForeignKey('mailing_list_id', 'mailing_lists', 'id', ['delete' => 'CASCADE', 'update' => 'CASCADE'])
            ->addForeignKey('individual_id', 'individuals', 'id', ['delete' => 'CASCADE', 'update' => 'CASCADE']);

        $mailinglists_individuals->addIndex(['mailing_list_id', 'individual_id'], ['unique' => true]);

        $mailinglists_individuals->create();


        $mailinglists_metafields = $this->table('mailing_lists_meta_fields', [
            'signed' => false,
            'collation' => 'utf8mb4_unicode_ci',
        ]);

        $mailinglists_metafields
            ->addColumn('mailing_list_id', 'integer', [
                'default' => null,
                'null' => true,
                'signed' => false,
                'length' => 10,
            ])
            ->addColumn('meta_field_id', 'integer', [
                'default' => null,
                'null' => true,
                'signed' => false,
                'length' => 10,
            ])
            ->addPrimaryKey(['mailing_list_id', 'meta_field_id'])
            ->addForeignKey('mailing_list_id', 'mailing_lists', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE'])
            ->addForeignKey('meta_field_id', 'meta_fields', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE']);

        $mailinglists_metafields->create();
    }
}

