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

const QATest = sequelize.define('QATest', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  projectId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'projects',
      key: 'id'
    }
  },
  userId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'users',
      key: 'id'
    }
  },
  testType: {
    type: DataTypes.ENUM('screenshot-compare', 'test-run'),
    allowNull: false
  },
  status: {
    type: DataTypes.ENUM('queued', 'running', 'completed', 'failed'),
    defaultValue: 'queued',
    index: true
  },
  baselineScreenshots: {
    type: DataTypes.JSON,
    defaultValue: null
  },
  currentScreenshots: {
    type: DataTypes.JSON,
    defaultValue: null
  },
  comparisonResults: {
    type: DataTypes.JSON,
    defaultValue: null
  },
  testSuite: {
    type: DataTypes.STRING,
    allowNull: true
  },
  testResults: {
    type: DataTypes.JSON,
    defaultValue: null
  },
  passedTests: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  failedTests: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  skippedTests: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  totalDuration: {
    type: DataTypes.INTEGER,
    allowNull: true
  },
  diffPercentage: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  error: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  metadata: {
    type: DataTypes.JSON,
    defaultValue: {}
  },
  startedAt: {
    type: DataTypes.DATE,
    allowNull: true
  },
  completedAt: {
    type: DataTypes.DATE,
    allowNull: true
  }
}, {
  tableName: 'qa_tests',
  timestamps: true,
  indexes: [
    { fields: ['projectId', 'createdAt'] },
    { fields: ['userId', 'status'] },
    { fields: ['status'] },
    { fields: ['testType'] }
  ]
});

export default QATest;
