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

const Book = sequelize.define('Book', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    title: {
        type: DataTypes.STRING(255),
        allowNull: true
    },
    author: {
        type: DataTypes.STRING(100),
        allowNull: true
    },
    publisher: {
        type: DataTypes.STRING(100),
        allowNull: true
    },
    publish_date: {
        type: DataTypes.DATEONLY,
        allowNull: true
    },
    isbn: {
        type: DataTypes.STRING(20),
        allowNull: true
    },
    price: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: true
    },
    originalPrice: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: true,
        field: 'originalPrice' // Sequelize default but good to be explicit if mixed case
    },
    category: {
        type: DataTypes.STRING(50),
        allowNull: true
    },
    image: {
        type: DataTypes.STRING(255),
        allowNull: true
    },
    description: {
        type: DataTypes.TEXT,
        allowNull: true
    },
    rating: {
        type: DataTypes.DECIMAL(3, 1),
        defaultValue: 0.0
    },
    discount: {
        type: DataTypes.INTEGER,
        defaultValue: 0
    },
    stock: {
        type: DataTypes.INTEGER,
        defaultValue: 0
    },

    tags: {
        type: DataTypes.JSON,
        allowNull: true
    }
}, {
    timestamps: true,
    tableName: 'books',
    underscored: true // This might affect originalPrice -> original_price mapping. 
    // The user wrote "originalPrice" (camelCase) in the column list.
    // But "Tipe Data: decimal...". 
    // Usually DB columns are snake_case.
    // However, if I use `underscored: true`, it expects `original_price`.
    // I will explicitly set the field name if needed, or disable underscored for this specific field if possible.
    // Actually, `field: 'originalPrice'` in definition overrides everything.
    // But usually standard is snake_case in DB, camelCase in JS.
    // Creating it as `originalPrice` in DB column to match request text literally?
    // "Nama Kolom ... originalPrice".
    // Okay, I will make the DB column `originalPrice`.
});

module.exports = Book;
