<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateFigmaTables extends Migration
{
	public function up()
	{
		$this->forge->addField([
			'id' => [
				'type' => 'BIGINT',
				'constraint' => 20,
				'unsigned' => true,
				'auto_increment' => true,
			],
			'file_key' => [
				'type' => 'VARCHAR',
				'constraint' => 255,
			],
			'name' => [
				'type' => 'VARCHAR',
				'constraint' => 255,
			],
			'version' => [
				'type' => 'VARCHAR',
				'constraint' => 255,
				'null' => true,
			],
			'last_modified' => [
				'type' => 'DATETIME',
				'null' => true,
			],
			'editor_type' => [
				'type' => 'VARCHAR',
				'constraint' => 100,
				'null' => true,
			],
			'last_synced_at' => [
				'type' => 'DATETIME',
				'null' => true,
			],
			'created_at' => [
				'type' => 'DATETIME',
				'null' => true,
			],
			'updated_at' => [
				'type' => 'DATETIME',
				'null' => true,
			],
		]);
		$this->forge->addKey('id', true);
		$this->forge->addUniqueKey('file_key');
		$this->forge->createTable('figma_files', true);

		$this->forge->addField([
			'id' => [
				'type' => 'BIGINT',
				'constraint' => 20,
				'unsigned' => true,
				'auto_increment' => true,
			],
			'file_id' => [
				'type' => 'BIGINT',
				'constraint' => 20,
				'unsigned' => true,
			],
			'figma_version_id' => [
				'type' => 'VARCHAR',
				'constraint' => 255,
			],
			'version' => [
				'type' => 'VARCHAR',
				'constraint' => 255,
				'null' => true,
			],
			'name' => [
				'type' => 'VARCHAR',
				'constraint' => 255,
				'null' => true,
			],
			'editor_type' => [
				'type' => 'VARCHAR',
				'constraint' => 100,
				'null' => true,
			],
			'last_modified_figma' => [
				'type' => 'DATETIME',
				'null' => true,
			],
			'created_at_figma' => [
				'type' => 'DATETIME',
				'null' => true,
			],
			'created_at' => [
				'type' => 'DATETIME',
				'null' => true,
			],
			'updated_at' => [
				'type' => 'DATETIME',
				'null' => true,
			],
		]);
		$this->forge->addKey('id', true);
		$this->forge->addUniqueKey(['file_id', 'figma_version_id']);
		$this->forge->addKey('file_id');
		$this->forge->addKey('created_at_figma');
		$this->forge->addKey('last_modified_figma');
		$this->forge->createTable('figma_file_versions', true);

		$this->forge->addField([
			'id' => [
				'type' => 'BIGINT',
				'constraint' => 20,
				'unsigned' => true,
				'auto_increment' => true,
			],
			'file_id' => [
				'type' => 'BIGINT',
				'constraint' => 20,
				'unsigned' => true,
			],
			'figma_comment_id' => [
				'type' => 'VARCHAR',
				'constraint' => 255,
			],
			'user_name' => [
				'type' => 'VARCHAR',
				'constraint' => 255,
				'null' => true,
			],
			'message' => [
				'type' => 'LONGTEXT',
				'null' => true,
			],
			'is_resolved' => [
				'type' => 'TINYINT',
				'constraint' => 1,
				'default' => 0,
			],
			'resolved_at' => [
				'type' => 'DATETIME',
				'null' => true,
			],
			'created_at_figma' => [
				'type' => 'DATETIME',
				'null' => true,
			],
			'client_meta_json' => [
				'type' => 'LONGTEXT',
				'null' => true,
			],
			'created_at' => [
				'type' => 'DATETIME',
				'null' => true,
			],
			'updated_at' => [
				'type' => 'DATETIME',
				'null' => true,
			],
		]);
		$this->forge->addKey('id', true);
		$this->forge->addUniqueKey('figma_comment_id');
		$this->forge->addKey('file_id');
		$this->forge->addKey('created_at_figma');
		$this->forge->createTable('figma_comments', true);
	}

	public function down()
	{
		$this->forge->dropTable('figma_comments', true);
		$this->forge->dropTable('figma_file_versions', true);
		$this->forge->dropTable('figma_files', true);
	}
}
