const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {

  const MarketStats = sequelize.define('MarketStats', {

    id: {
      type: DataTypes.BIGINT,
      autoIncrement: true,
      primaryKey: true
    },

    pairId: {
      type: DataTypes.BIGINT,
      unique: true,
      field: 'pair_id'
    },

    volume24h: {
      type: DataTypes.DECIMAL(28, 10)
    },

    marketCap: {
      type: DataTypes.DECIMAL(30, 2),
      field: 'market_cap'
    },

    // 🔹 Crypto advanced metrics
    fullyDilutedValuation: {
      type: DataTypes.DECIMAL(30, 2),
      field: 'fully_diluted_valuation'
    },

    volumeToMarketCap: {
      type: DataTypes.DECIMAL(10, 6),
      field: 'volume_to_market_cap'
    },

    ath: {
      type: DataTypes.DECIMAL(20, 10),
      field: 'all_time_high'
    },

    atl: {
      type: DataTypes.DECIMAL(20, 10),
      field: 'all_time_low'
    },

    athDate: {
      type: DataTypes.DATE,
      field: 'ath_date'
    },

    atlDate: {
      type: DataTypes.DATE,
      field: 'atl_date'
    },

    athChange: {
      type: DataTypes.DECIMAL(10, 4),
      field: 'ath_change_percent'
    },

    atlChange: {
      type: DataTypes.DECIMAL(10, 4),
      field: 'atl_change_percent'
    },

    changePercent1h: {
      type: DataTypes.DECIMAL(10, 4),
      field: 'change_percent_1h'
    },

    changePercent7d: {
      type: DataTypes.DECIMAL(10, 4),
      field: 'change_percent_7d'
    },

    cmcRank: {
      type: DataTypes.INTEGER,
      field: 'cmc_rank'
    },

    // 🔹 Supply
    circulatingSupply: {
      type: DataTypes.DECIMAL(30, 10),
      field: 'circulating_supply'
    },

    totalSupply: {
      type: DataTypes.DECIMAL(30, 10),
      field: 'total_supply'
    },

    maxSupply: {
      type: DataTypes.DECIMAL(30, 10),
      field: 'max_supply'
    },

    // 🔹 Derivatives / commodities
    openInterest: {
      type: DataTypes.DECIMAL(30, 10),
      field: 'open_interest'
    },

    contractSize: {
      type: DataTypes.DECIMAL(20, 10),
      field: 'contract_size'
    },

    // 🔹 Stock fundamentals
    peRatio: {
      type: DataTypes.DECIMAL(10, 4),
      field: 'pe_ratio'
    },

    pegRatio: {
      type: DataTypes.STRING(100),
      field: 'peg_ratio'
    },

    eps: {
      type: DataTypes.DECIMAL(10, 4)
    },

    ebitda: {
      type: DataTypes.STRING(100)
    },

    dividendPerShare: {
      type: DataTypes.STRING(100),
      field: 'dividend_per_share'
    },

    dividendYield: {
      type: DataTypes.STRING(20),
      field: 'dividend_yield'
    },

    revenue: {
      type: DataTypes.STRING(100),
      field: 'revenue'
    },


    revenuePerShare: {
      type: DataTypes.DECIMAL(20, 8),
      field: 'revenue_per_share'
    },

    operatingMargin: {
      type: DataTypes.STRING(100),
      field: 'operating_margin'
    },

    grossProfit: {
      type: DataTypes.STRING(100),
      field: 'gross_profit'
    },

    profitMargin: {
      type: DataTypes.STRING(100),
      field: 'profit_margin'
    },


    returnOnAssets: {
      type: DataTypes.STRING(100),
      field: 'return_on_assets'
    },

    returnOnEquity: {
      type: DataTypes.STRING(100),
      field: 'return_on_equity'
    },



    // 🔹 52 week
    high52w: {
      type: DataTypes.DECIMAL(20, 10)
    },

    low52w: {
      type: DataTypes.DECIMAL(20, 10)
    }

  }, {
    tableName: 'market_stats',
    timestamps: true,
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  });

  MarketStats.prototype.toJSON = function () {
    const values = { ...this.get() };
    return values;
  };
  return MarketStats;
};