import { Migration } from '@mikro-orm/migrations';

export class Migration20240416000001 extends Migration {
    async up(): Promise<void> {
        this.addSql(`
            create table "prompt" (
                "id" serial primary key,
                "created_at" timestamptz not null,
                "updated_at" timestamptz not null,
                "name" text null,
                "content" text not null,
                "metadata" jsonb not null default '{}',
                "type" varchar(50) not null,
                "status" varchar(50) not null default 'pending',
                "user_id" int not null,
                "app_id" int not null,
                "processed_result" jsonb null,
                "error_message" text null
            );
        `);

        this.addSql(`
            alter table "prompt" 
            add constraint "prompt_user_id_foreign" 
            foreign key ("user_id") 
            references "user" ("id") 
            on update cascade 
            on delete cascade;
        `);

        this.addSql(`
            alter table "prompt" 
            add constraint "prompt_app_id_foreign" 
            foreign key ("app_id") 
            references "app" ("id") 
            on update cascade 
            on delete cascade;
        `);

        // Add indexes for common queries
        this.addSql('create index "prompt_user_id_index" on "prompt" ("user_id");');
        this.addSql('create index "prompt_app_id_index" on "prompt" ("app_id");');
        this.addSql('create index "prompt_status_index" on "prompt" ("status");');
        this.addSql('create index "prompt_type_index" on "prompt" ("type");');
    }

    async down(): Promise<void> {
        this.addSql('drop table if exists "prompt" cascade;');
    }
} 