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

module.exports = (sequelize) => {
  const BotMarketplace = sequelize.define(
    "BotMarketplace",
    {
      id: {
        type: DataTypes.BIGINT.UNSIGNED,
        autoIncrement: true,
        allowNull: false,
        primaryKey: true,
      },
      reference: { type: DataTypes.STRING(20), allowNull: false, defaultValue: () => generateRandomString(16) },
      // Core info
      name: { type: DataTypes.STRING(150), allowNull: false },
      description: { type: DataTypes.TEXT, allowNull: false },

      category: {
        type: DataTypes.ENUM("grid", "dca", "arbitrage", "scalping", "trend", "custom"),
        allowNull: false,
      },

      // Added: strategy type
      strategyType: {
        type: DataTypes.ENUM("rule_based", "ai", "hybrid"),
        defaultValue: "rule_based",
        field: "strategy_type",
      },

      // Creator
      creator: { type: DataTypes.STRING(120), allowNull: false },

      // Added: creatorId (FK later)
      creatorId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: true,
        field: "creator_id",
      },

      creatorVerified: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
        field: "creator_verified",
      },

      // Ratings
      rating: { type: DataTypes.DECIMAL(3, 2), defaultValue: 0 },
      reviews: { type: DataTypes.INTEGER.UNSIGNED, defaultValue: 0 },
      users: { type: DataTypes.INTEGER.UNSIGNED, defaultValue: 0 },

      // Pricing
      price: { type: DataTypes.DECIMAL(10, 2), allowNull: false, defaultValue: 0 },

      pricePeriod: {
        type: DataTypes.ENUM("day", "month", "year", "lifetime"),
        defaultValue: "month",
        field: "price_period",
      },

      // Added: subscription controls
      isSubscription: {
        type: DataTypes.BOOLEAN,
        defaultValue: true,
        field: "is_subscription",
      },

      trialDays: {
        type: DataTypes.INTEGER.UNSIGNED,
        defaultValue: 0,
        field: "trial_days",
      },

      tier: {
        type: DataTypes.ENUM("standard", "premium"),
        defaultValue: "standard",
      },

      // Added: revenue fields
      totalRevenue: {
        type: DataTypes.DECIMAL(20, 2),
        defaultValue: 0,
        field: "total_revenue",
      },

      revenueShare: {
        type: DataTypes.DECIMAL(5, 2),
        defaultValue: 70, // %
        field: "revenue_share",
      },

      // Performance (fix: JSON, queryable)
      performance: {
        type: DataTypes.JSON,
        allowNull: false,
        defaultValue: {}, // { roi, winRate, maxDrawdown, sharpeRatio, ... }
      },

      // Trading universe
      supportedPairs: {
        type: DataTypes.JSON,
        allowNull: false,
        field: "supported_pairs",
        defaultValue: [],
      },

      supportedExchanges: {
        type: DataTypes.JSON,
        allowNull: false,
        field: "supported_exchanges",
        defaultValue: [],
      },

      features: {
        type: DataTypes.JSON,
        allowNull: false,
        defaultValue: [],
      },

      minCapital: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
        field: "min_capital",
        defaultValue: 0,
      },

      tags: { type: DataTypes.JSON, defaultValue: [] },

      // Strategy content
      strategyCode: {
        type: DataTypes.TEXT("long"),
        allowNull: false,
        field: "strategy_code",
      },

      defaultConfig: {
        type: DataTypes.JSON,
        allowNull: false,
        field: "default_config",
        defaultValue: {},
      },

      chartData: { type: DataTypes.JSON, field: "chart_data" },

      // Added: risk + audit
      riskLevel: {
        type: DataTypes.ENUM("low", "medium", "high"),
        defaultValue: "medium",
        field: "risk_level",
      },

      isAudited: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
        field: "is_audited",
      },

      auditScore: {
        type: DataTypes.DECIMAL(3, 2),
        allowNull: true,
        field: "audit_score",
      },

      // Added: versioning
      version: { type: DataTypes.STRING(32), defaultValue: "1.0.0" },

      changelog: { type: DataTypes.TEXT, allowNull: true },

      isDeprecated: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
        field: "is_deprecated",
      },

      // Visibility
      isPublished: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
        field: "is_published",
      },

      isFeatured: {
        type: DataTypes.BOOLEAN,
        defaultValue: false,
        field: "is_featured",
      },

      // Marketplace metrics
      downloads: { type: DataTypes.INTEGER.UNSIGNED, defaultValue: 0 },

      activeInstances: {
        type: DataTypes.INTEGER.UNSIGNED,
        defaultValue: 0,
        field: "active_instances",
      },

      // Added: analytics
      views: { type: DataTypes.INTEGER.UNSIGNED, defaultValue: 0 },

      conversionRate: {
        type: DataTypes.DECIMAL(5, 2),
        allowNull: true,
        field: "conversion_rate",
      },

      refundRate: {
        type: DataTypes.DECIMAL(5, 2),
        allowNull: true,
        field: "refund_rate",
      },
    },
    {
      tableName: "bot_marketplace",
      underscored: true,
      timestamps: true,
      indexes: [
        { fields: ["category"] },
        { fields: ["tier"] },
        { fields: ["is_published"] },
        { fields: ["is_featured"] },
        { fields: ["rating"] },
        { fields: ["creator_id"] },
        { fields: ["strategy_type"] },
        { fields: ["risk_level"] },
      ],
    }
  );

  return BotMarketplace;
};
