import { MigrationInterface, QueryRunner } from 'typeorm';

export class addLicenseTables1702918857719 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
            CREATE TABLE license_types(
                id serial PRIMARY KEY,
                name varchar NOT NULL,
                title varchar NOT NULL,
                features JSONB NOT NULL,
                priority integer NOT NULL DEFAULT(1)
            );        
        `);
    await queryRunner.query(`
            CREATE TABLE license_plans(
                id serial PRIMARY KEY,
                title varchar NOT NULL,
                license_type_id INT NOT NULL REFERENCES license_types(id) ON UPDATE CASCADE ON DELETE RESTRICT,
                month_num integer,
                shop_info JSONB,
                price_rub decimal(14,2),
                price_usd decimal(14,2),
                index integer
            );        
        `);
    await queryRunner.query(`
            CREATE TABLE project_licenses(
                id serial PRIMARY KEY,
                project_id BIGINT NOT NULL REFERENCES projects(id) ON UPDATE CASCADE ON DELETE RESTRICT,
                license_plan_id INT NOT NULL REFERENCES license_plans(id) ON UPDATE CASCADE ON DELETE RESTRICT,
                start_at timestamp with time zone NOT NULL DEFAULT(NOW()),
                till timestamp with time zone,
                updated_at timestamp with time zone NOT NULL DEFAULT(NOW()),
                payment_info JSONB,
                features JSONB,
                comment TEXT,
                is_trial BOOLEAN NOT NULL DEFAULT(false)
            );        
        `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
            DROP TABLE project_licenses;
        `);
    await queryRunner.query(`
            DROP TABLE license_plans;
        `);
    await queryRunner.query(`
            DROP TABLE license_types;
        `);
  }
}
