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

const Voucher = sequelize.define('Voucher', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    code: {
        type: DataTypes.STRING,
        unique: true,
        allowNull: false
    },
    discount: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: false
    },
    type: {
        type: DataTypes.ENUM('Percentage', 'Fixed'),
        allowNull: false
    },
    minPurchase: {
        type: DataTypes.DECIMAL(10, 2),
        defaultValue: 0
    },
    maxDiscount: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: true
    },
    expiryDate: {
        type: DataTypes.DATE,
        allowNull: false
    },
    usageLimit: {
        type: DataTypes.INTEGER,
        defaultValue: 100
    },
    usedCount: {
        type: DataTypes.INTEGER,
        defaultValue: 0
    },
    status: {
        type: DataTypes.ENUM('Active', 'Expired', 'Inactive'),
        defaultValue: 'Active'
    },
    description: {
        type: DataTypes.STRING,
        allowNull: true
    },
    applicable_tags: {
        type: DataTypes.JSON, // Array of strings e.g. ["Fiksi", "Novel"]
        allowNull: true
    }
}, {
    timestamps: true
});

module.exports = Voucher;
