import { DataTypes } from 'sequelize';
import sequelize from '../config/database.js';

const Discovery = sequelize.define('Discovery', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  projectId: {
    type: DataTypes.UUID,
    allowNull: true,
    references: {
      model: 'projects',
      key: 'id'
    }
  },
  userId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'users',
      key: 'id'
    }
  },
  type: {
    type: DataTypes.ENUM('project-analysis', 'quick-wins', 'component-audit', 'token-extraction'),
    index: true
  },
  status: {
    type: DataTypes.ENUM('queued', 'running', 'completed', 'failed'),
    defaultValue: 'queued',
    index: true
  },
  progress: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  results: {
    type: DataTypes.JSON,
    defaultValue: null
  },
  error: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  metadata: {
    type: DataTypes.JSON,
    defaultValue: {}
  },
  startedAt: {
    type: DataTypes.DATE,
    allowNull: true
  },
  completedAt: {
    type: DataTypes.DATE,
    allowNull: true
  }
}, {
  tableName: 'discoveries',
  timestamps: true,
  indexes: [
    { fields: ['userId', 'createdAt'] },
    { fields: ['projectId', 'status'] },
    { fields: ['status'] }
  ]
});

export default Discovery;
