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

const Component = sequelize.define('Component', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  projectId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'Projects',
      key: 'id'
    }
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  category: {
    type: DataTypes.STRING,
    allowNull: true
  },
  figmaId: {
    type: DataTypes.STRING,
    allowNull: true,
    unique: true
  },
  storybookPath: {
    type: DataTypes.STRING,
    allowNull: true
  },
  status: {
    type: DataTypes.ENUM('draft', 'wip', 'ready', 'deprecated'),
    defaultValue: 'draft'
  },
  adoptionScore: {
    type: DataTypes.FLOAT,
    defaultValue: 0
  },
  usageCount: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  variants: {
    type: DataTypes.JSON,
    defaultValue: []
  },
  metadata: {
    type: DataTypes.JSON,
    allowNull: true
  },
  createdAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  }
}, {
  timestamps: true,
  indexes: [
    { fields: ['projectId'] },
    { fields: ['status'] }
  ]
});

export default Component;
