import { MigrationInterface, QueryRunner } from "typeorm"

export class InitialMigration1761647374804 implements MigrationInterface {
  name = "InitialMigration1761647374804"

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      'CREATE TABLE "user_access_tokens" ("id" SERIAL NOT NULL, "user_id" integer NOT NULL, "device_id" character varying, "device_token" character varying, "access_token" character varying NOT NULL, "access_token_expire_at" TIMESTAMP NOT NULL, "created_at" TIMESTAMP DEFAULT NOW(), "updated_at" TIMESTAMP DEFAULT NOW(), CONSTRAINT "PK_f07c49baf74e5d699c83e2ec2bd" PRIMARY KEY ("id"))',
    )
    await queryRunner.query(
      'CREATE TABLE "users" ("id" SERIAL NOT NULL, "role_id" integer NOT NULL, "first_name" character varying NOT NULL, "last_name" character varying, "slug" character varying NOT NULL, "email" character varying NOT NULL, "password" character varying, "contact_no" character varying, "profile_pic" character varying, "status" smallint NOT NULL, "deleted_at" TIMESTAMP, "created_at" TIMESTAMP DEFAULT NOW(), "updated_at" TIMESTAMP DEFAULT NOW(), CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3" UNIQUE ("email"), CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))',
    )
    await queryRunner.query(
      'CREATE TABLE "roles" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "status" smallint NOT NULL, "deleted_at" TIMESTAMP, "created_at" TIMESTAMP DEFAULT NOW(), "updated_at" TIMESTAMP DEFAULT NOW(), CONSTRAINT "PK_c1433d71a4838793a49dcad46ab" PRIMARY KEY ("id"))',
    )
    await queryRunner.query(
      'CREATE TABLE "categories" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "status" smallint NOT NULL, "deleted_at" TIMESTAMP, "created_at" TIMESTAMP DEFAULT NOW(), "updated_at" TIMESTAMP DEFAULT NOW(), CONSTRAINT "PK_24dbc6126a28ff948da33e97d3b" PRIMARY KEY ("id"))',
    )
    await queryRunner.query(
      'CREATE TABLE "api_logs" ("id" SERIAL NOT NULL, "header" jsonb, "form_data" jsonb, "response_code" integer, "response" jsonb, "url" character varying, "response_time" character varying, "created_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_ea3f2ad34a2921407593ff4425b" PRIMARY KEY ("id"))',
    )
    await queryRunner.query(
      'ALTER TABLE "user_access_tokens" ADD CONSTRAINT "FK_e9d9d0c303432e4e5e48c1c3e90" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION',
    )
    await queryRunner.query(
      'ALTER TABLE "users" ADD CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1" FOREIGN KEY ("role_id") REFERENCES "roles"("id") ON DELETE NO ACTION ON UPDATE NO ACTION',
    )
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      'ALTER TABLE "users" DROP CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1"',
    )
    await queryRunner.query(
      'ALTER TABLE "user_access_tokens" DROP CONSTRAINT "FK_e9d9d0c303432e4e5e48c1c3e90"',
    )
    await queryRunner.query('DROP TABLE "api_logs"')
    await queryRunner.query('DROP TABLE "categories"')
    await queryRunner.query('DROP TABLE "roles"')
    await queryRunner.query('DROP TABLE "users"')
    await queryRunner.query('DROP TABLE "user_access_tokens"')
  }
}
