"use strict";
const { DataTypes } = require("sequelize");
const { generateRandomString } = require("../utils/helpers");

module.exports = (sequelize) => {
    const BotInstance = sequelize.define(
        "BotInstance",
        {
            id: { type: DataTypes.BIGINT.UNSIGNED, autoIncrement: true, primaryKey: true },
            reference: {type: DataTypes.STRING(20), allowNull: false, defaultValue: () => generateRandomString(16)},

            marketplaceBotId: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                field: "marketplace_bot_id",
            },

            userId: { type: DataTypes.BIGINT.UNSIGNED, allowNull: false, field: "user_id" },

            exchangeAccountId: {
                type: DataTypes.BIGINT.UNSIGNED,
                field: "exchange_account_id",
            },

            symbol: { type: DataTypes.STRING(30) },

            status: {
                type: DataTypes.ENUM("stopped", "running", "paused", "error"),
                allowNull: false,
                defaultValue: "stopped",
            },

            config: { type: DataTypes.JSON, allowNull: false, defaultValue: {} },
            state: { type: DataTypes.JSON, allowNull: false, defaultValue: {} },

            lastHeartbeatAt: { type: DataTypes.DATE, allowNull: true, field: "last_heartbeat_at" },
            nextRunAt: { type: DataTypes.DATE, allowNull: true, field: "next_run_at" },

            assignedWorkerId: { type: DataTypes.STRING(64), allowNull: true, field: "assigned_worker_id" },

            errorCode: { type: DataTypes.STRING(80), allowNull: true, field: "error_code" },
            errorMessage: { type: DataTypes.TEXT, allowNull: true, field: "error_message" },
            // Add inside BotInstance fields
            billingStatus: {
                type: DataTypes.ENUM("active", "cancelled", "expired", "suspended"),
                allowNull: false,
                defaultValue: "active",
                field: "billing_status",
            },
            startDate: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW,
                field: "start_date",
            },
            endDate: { type: DataTypes.DATE, allowNull: true, field: "end_date" },
            autoRenew: { type: DataTypes.BOOLEAN, defaultValue: true, field: "auto_renew" },
            price: { type: DataTypes.DECIMAL(10, 2), allowNull: false, defaultValue: 0 },
            pricePeriod: {
                type: DataTypes.ENUM("day", "month", "year", "lifetime"),
                allowNull: false,
                defaultValue: "month",
                field: "price_period",
            },
            lastPaymentDate: { type: DataTypes.DATE, allowNull: true, field: "last_payment_date" },
            nextPaymentDate: { type: DataTypes.DATE, allowNull: true, field: "next_payment_date" },
            userRating: { type: DataTypes.TINYINT.UNSIGNED, allowNull: true, field: "user_rating" }, // 1-5
            userReview: { type: DataTypes.TEXT, allowNull: true, field: "user_review" },
            reviewedAt: { type: DataTypes.DATE, allowNull: true, field: "reviewed_at" },

        },
        {
            tableName: "bot_instances",
            underscored: true,
            timestamps: true,
            indexes: [
                { fields: ["user_id"] },
                { fields: ["marketplace_bot_id"] },
                { fields: ["exchange_account_id"] },
                { fields: ["status"] },
                { fields: ["symbol"] },
                { fields: ["next_run_at"] },
            ],
        }
    );

    return BotInstance;
};
