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

const FigmaSync = sequelize.define('FigmaSync', {
  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'
    }
  },
  figmaFileKey: {
    type: DataTypes.STRING,
    allowNull: false
  },
  figmaFileUrl: {
    type: DataTypes.STRING,
    allowNull: false
  },
  syncType: {
    type: DataTypes.ENUM('extract-variables', 'extract-components', 'extract-styles', 'sync-tokens', 'visual-diff', 'validate-components', 'generate-code', 'export-assets'),
    allowNull: false
  },
  status: {
    type: DataTypes.ENUM('queued', 'running', 'completed', 'failed'),
    defaultValue: 'queued',
    index: true
  },
  extractedData: {
    type: DataTypes.JSON,
    defaultValue: null
  },
  syncedTokens: {
    type: DataTypes.JSON,
    defaultValue: null
  },
  validationResults: {
    type: DataTypes.JSON,
    defaultValue: null
  },
  diffReport: {
    type: DataTypes.JSON,
    defaultValue: null
  },
  generatedCode: {
    type: DataTypes.TEXT,
    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: 'figma_syncs',
  timestamps: true,
  indexes: [
    { fields: ['projectId', 'createdAt'] },
    { fields: ['userId', 'status'] },
    { fields: ['status'] }
  ]
});

export default FigmaSync;
