module.exports = {
  async up(queryInterface, Sequelize) {
    const transaction = await queryInterface.sequelize.transaction();

    try {
      await queryInterface.createTable('users', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        firstName: { type: Sequelize.DataTypes.TEXT },
        lastName: { type: Sequelize.DataTypes.TEXT },
        phoneNumber: { type: Sequelize.DataTypes.TEXT },
        email: { type: Sequelize.DataTypes.TEXT },
        disabled: { type: Sequelize.DataTypes.BOOLEAN, allowNull: false, defaultValue: false },
        password: { type: Sequelize.DataTypes.TEXT },
        emailVerified: { type: Sequelize.DataTypes.BOOLEAN, allowNull: false, defaultValue: false },
        emailVerificationToken: { type: Sequelize.DataTypes.TEXT },
        emailVerificationTokenExpiresAt: { type: Sequelize.DataTypes.DATE },
        passwordResetToken: { type: Sequelize.DataTypes.TEXT },
        passwordResetTokenExpiresAt: { type: Sequelize.DataTypes.DATE },
        provider: { type: Sequelize.DataTypes.TEXT },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.createTable('roles', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        name: { type: Sequelize.DataTypes.TEXT },
        role_customization: { type: Sequelize.DataTypes.TEXT },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.createTable('permissions', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        name: { type: Sequelize.DataTypes.TEXT },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.addColumn('users', 'app_roleId', {
        type: Sequelize.DataTypes.UUID,
        references: { model: 'roles', key: 'id' },
      }, { transaction });

      await queryInterface.createTable('rolesPermissionsPermissions', {
        createdAt: { type: Sequelize.DataTypes.DATE, allowNull: false },
        updatedAt: { type: Sequelize.DataTypes.DATE, allowNull: false },
        roles_permissionsId: {
          type: Sequelize.DataTypes.UUID,
          allowNull: false,
          references: { model: 'roles', key: 'id' },
          primaryKey: true,
        },
        permissionId: {
          type: Sequelize.DataTypes.UUID,
          allowNull: false,
          references: { model: 'permissions', key: 'id' },
          primaryKey: true,
        },
      }, { transaction });

      await queryInterface.createTable('usersCustom_permissionsPermissions', {
        createdAt: { type: Sequelize.DataTypes.DATE, allowNull: false },
        updatedAt: { type: Sequelize.DataTypes.DATE, allowNull: false },
        users_custom_permissionsId: {
          type: Sequelize.DataTypes.UUID,
          allowNull: false,
          references: { model: 'users', key: 'id' },
          primaryKey: true,
        },
        permissionId: {
          type: Sequelize.DataTypes.UUID,
          allowNull: false,
          references: { model: 'permissions', key: 'id' },
          primaryKey: true,
        },
      }, { transaction });

      await queryInterface.createTable('packages', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        title: { type: Sequelize.DataTypes.TEXT },
        description: { type: Sequelize.DataTypes.TEXT },
        price: { type: Sequelize.DataTypes.TEXT },
        duration: { type: Sequelize.DataTypes.TEXT },
        cta: { type: Sequelize.DataTypes.TEXT },
        included_sessions: { type: Sequelize.DataTypes.INTEGER },
        is_active: { type: Sequelize.DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.createTable('clients', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        name: { type: Sequelize.DataTypes.TEXT },
        email: { type: Sequelize.DataTypes.TEXT },
        status: { type: Sequelize.DataTypes.ENUM('lead', 'active', 'paused', 'completed'), allowNull: false, defaultValue: 'active' },
        goals: { type: Sequelize.DataTypes.TEXT },
        notes: { type: Sequelize.DataTypes.TEXT },
        company: { type: Sequelize.DataTypes.TEXT },
        role_title: { type: Sequelize.DataTypes.TEXT },
        tags: { type: Sequelize.DataTypes.TEXT },
        next_session_at: { type: Sequelize.DataTypes.DATE },
        last_session_at: { type: Sequelize.DataTypes.DATE },
        packageId: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'packages' } },
        ownerId: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.createTable('intake_leads', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        name: { type: Sequelize.DataTypes.TEXT },
        email: { type: Sequelize.DataTypes.TEXT },
        company: { type: Sequelize.DataTypes.TEXT },
        role_title: { type: Sequelize.DataTypes.TEXT },
        goal: { type: Sequelize.DataTypes.TEXT },
        challenge: { type: Sequelize.DataTypes.TEXT },
        desired_outcome: { type: Sequelize.DataTypes.TEXT },
        source: { type: Sequelize.DataTypes.TEXT },
        status: { type: Sequelize.DataTypes.ENUM('new', 'reviewed', 'invited', 'converted', 'archived'), allowNull: false, defaultValue: 'new' },
        consent_ai_notes: { type: Sequelize.DataTypes.BOOLEAN, allowNull: false, defaultValue: false },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.createTable('sessions', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        clientId: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'clients' } },
        title: { type: Sequelize.DataTypes.TEXT },
        session_at: { type: Sequelize.DataTypes.DATE },
        status: { type: Sequelize.DataTypes.ENUM('planned', 'completed', 'draft_review', 'shared'), allowNull: false, defaultValue: 'completed' },
        transcript_notes: { type: Sequelize.DataTypes.TEXT },
        ai_summary: { type: Sequelize.DataTypes.TEXT },
        key_topics: { type: Sequelize.DataTypes.TEXT },
        goals_discussed: { type: Sequelize.DataTypes.TEXT },
        blockers: { type: Sequelize.DataTypes.TEXT },
        commitments: { type: Sequelize.DataTypes.TEXT },
        homework: { type: Sequelize.DataTypes.TEXT },
        emotional_themes: { type: Sequelize.DataTypes.TEXT },
        important_quotes: { type: Sequelize.DataTypes.TEXT },
        follow_up_email: { type: Sequelize.DataTypes.TEXT },
        next_session_prep: { type: Sequelize.DataTypes.TEXT },
        private_coach_notes: { type: Sequelize.DataTypes.TEXT },
        shared_client_notes: { type: Sequelize.DataTypes.TEXT },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.createTable('action_items', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        clientId: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'clients' } },
        sessionId: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'sessions' } },
        title: { type: Sequelize.DataTypes.TEXT },
        due_at: { type: Sequelize.DataTypes.DATE },
        status: { type: Sequelize.DataTypes.ENUM('not_started', 'in_progress', 'done'), allowNull: false, defaultValue: 'not_started' },
        notes: { type: Sequelize.DataTypes.TEXT },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.createTable('resources', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        clientId: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'clients' } },
        packageId: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'packages' } },
        title: { type: Sequelize.DataTypes.TEXT },
        description: { type: Sequelize.DataTypes.TEXT },
        url: { type: Sequelize.DataTypes.TEXT },
        resource_type: { type: Sequelize.DataTypes.ENUM('link', 'worksheet', 'pdf', 'video'), allowNull: false, defaultValue: 'link' },
        is_shared: { type: Sequelize.DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.createTable('testimonials', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        name: { type: Sequelize.DataTypes.TEXT },
        role_company: { type: Sequelize.DataTypes.TEXT },
        quote: { type: Sequelize.DataTypes.TEXT },
        photo_url: { type: Sequelize.DataTypes.TEXT },
        visible_on_site: { type: Sequelize.DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await queryInterface.createTable('prep_briefs', {
        id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.DataTypes.UUIDV4, primaryKey: true },
        clientId: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'clients' } },
        sessionId: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'sessions' } },
        next_session_at: { type: Sequelize.DataTypes.DATE },
        previous_summary: { type: Sequelize.DataTypes.TEXT },
        open_commitments: { type: Sequelize.DataTypes.TEXT },
        suggested_questions: { type: Sequelize.DataTypes.TEXT },
        sensitive_topics: { type: Sequelize.DataTypes.TEXT },
        status: { type: Sequelize.DataTypes.ENUM('draft', 'ready', 'archived'), allowNull: false, defaultValue: 'ready' },
        importHash: { type: Sequelize.DataTypes.STRING(255), allowNull: true, unique: true },
        createdById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        updatedById: { type: Sequelize.DataTypes.UUID, references: { key: 'id', model: 'users' } },
        createdAt: { type: Sequelize.DataTypes.DATE },
        updatedAt: { type: Sequelize.DataTypes.DATE },
        deletedAt: { type: Sequelize.DataTypes.DATE },
      }, { transaction });

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();
      throw error;
    }
  },

  async down(queryInterface) {
    const transaction = await queryInterface.sequelize.transaction();
    try {
      const tables = [
        'prep_briefs',
        'testimonials',
        'resources',
        'action_items',
        'sessions',
        'intake_leads',
        'clients',
        'packages',
        'usersCustom_permissionsPermissions',
        'rolesPermissionsPermissions',
        'permissions',
        'roles',
        'users',
      ];

      for (const table of tables) {
        await queryInterface.dropTable(table, { transaction });
      }

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();
      throw error;
    }
  },
};
