const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const Service = sequelize.define('Service', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      len: [1, 255]
    },
    set(value) {
      this.setDataValue('name', value.trim());
    }
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: false
  },
  shortDescription: {
    type: DataTypes.STRING(150),
    allowNull: false
  },
  icon: {
    type: DataTypes.STRING,
    allowNull: false
  },
  category: {
    type: DataTypes.ENUM(
      'web-development', 
      'mobile-development', 
      'ui-ux-design', 
      'consulting', 
      'support', 
      'other'
    ),
    allowNull: false
  },
  features: {
    type: DataTypes.JSONB,
    defaultValue: []
  },
  pricing: {
    type: DataTypes.JSONB,
    allowNull: false,
    validate: {
      isValidPricing(value) {
        if (!value.basePrice || value.basePrice < 0) {
          throw new Error('Base price must be a positive number');
        }
      }
    }
  },
  estimatedTime: {
    type: DataTypes.STRING,
    allowNull: true
  },
  isActive: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  },
  featured: {
    type: DataTypes.BOOLEAN,
    defaultValue: false
  },
  order: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  tags: {
    type: DataTypes.JSONB,
    defaultValue: []
  },
  seo: {
    type: DataTypes.JSONB,
    defaultValue: {}
  }
}, {
  tableName: 'services',
  timestamps: true,
  indexes: [
    {
      fields: ['category', 'featured', 'order']
    },
    {
      type: 'gin',
      fields: ['tags']
    }
  ]
});

module.exports = Service;