import { ClassError, ObjectBase } from '@tomei/general';
import { ApplicationLog } from '@tomei/log';
import { FeedCutOffHistory } from '../feed-cut-off-history/feed-cut-off-history';
import { ISourceFeedAttr } from '../../interfaces/price-source-feed-attr.interface';
import { SourceFeedRepository } from './source-feed.repository';
import { ActivationTrigger, SourceFeedName } from '../../enum/feed.enum';
import { IPriceData } from '../../interfaces/price-data.interface';
import { FeedHistory } from '../feed-history/feed-history';
import { Feed } from '../feed/feed';
import { IFeedAttr } from '../../interfaces/price-feed-attr.interface';
import { FeedHistoryRepository } from '../feed-history/feed-history.repository';
import { Op } from 'sequelize';
import { LoginUser } from '@tomei/sso';
import { PriceSourceBase } from '../../base/PriceSourceBase';
import {
  ActionEnum,
  Activity,
  ActivityHistoryRepository,
} from '@tomei/activity-history';

export class SourceFeed extends Feed implements ISourceFeedAttr {
  ObjectId: string;
  ObjectName: string;
  ObjectType: string;
  TableName: string;
  FeedName: SourceFeedName;
  Status: string;
  IsSourceFeedAvailableYN: string;
  IsFeedCutOffYN: string;
  CurrentCutOffHistoryId: string;
  AutomaticCutOffVariance: number;
  AutomaticCutOffMinutes: number;

  protected static _SourceRepo = new SourceFeedRepository();
  protected static _FeedHistoryRepo = new FeedHistoryRepository();
  protected static _ActivityRepo = new ActivityHistoryRepository();
  protected static logger = new ApplicationLog('live-price', 'SourceFeed');

  protected constructor(
    sourcefeedAttr?: ISourceFeedAttr,
    feedAttr?: IFeedAttr,
  ) {
    super(feedAttr);
    if (sourcefeedAttr) {
      this.FeedName = sourcefeedAttr.FeedName;
      this.Status = sourcefeedAttr.Status;
      this.IsSourceFeedAvailableYN = sourcefeedAttr.IsSourceFeedAvailableYN ?? 'Y';
      this.IsFeedCutOffYN = sourcefeedAttr.IsFeedCutOffYN ?? 'N';
      this.IsManualPriceActivatedYN = feedAttr?.IsManualPriceActivatedYN ?? 'N';
      this.CurrentCutOffHistoryId = sourcefeedAttr.CurrentCutOffHistoryId;
      this.AutomaticCutOffVariance = sourcefeedAttr.AutomaticCutOffVariance;
      this.AutomaticCutOffMinutes = sourcefeedAttr.AutomaticCutOffMinutes;
    }
  }

  public static async init(dbTransaction: any, FeedName?: string) {
    try {
      if (FeedName) {
        const sourcefeed = await SourceFeed._SourceRepo.findByPk(
          FeedName,
          dbTransaction,
        );
        const feed = await Feed._Repo.findByPk(FeedName, dbTransaction);
        if (sourcefeed) {
          return new SourceFeed(sourcefeed, feed);
        } else {
          const error = new ClassError(
            'SourceFeed',
            'FeedErrMsg00',
            'Feed Not Found',
          );
          await SourceFeed.logger.error({
            error,
            methodName: 'init',
            transaction: dbTransaction,
          });
          throw error;
        }
      }
      return new SourceFeed();
    } catch (error) {
      const wrappedError = new ClassError(
        'SourceFeed',
        'FeedErrMsg01',
        error.message,
      );
      await SourceFeed.logger.error({
        error,
        methodName: 'init',
        transaction: dbTransaction,
      });
      throw wrappedError;
    }
  }

  public async save(dbTransaction: any) {
    try {
      await super.save(dbTransaction);
      const sourcefeed = await SourceFeed._SourceRepo.findOne({
        where: {
          FeedName: this.FeedName,
        },
        transaction: dbTransaction,
      });
      const payload: ISourceFeedAttr = {
        FeedName: this.FeedName,
        Status: this.Status,
        IsSourceFeedAvailableYN: this.IsSourceFeedAvailableYN,
        IsFeedCutOffYN: this.IsFeedCutOffYN,
        CurrentCutOffHistoryId: this.CurrentCutOffHistoryId,
        AutomaticCutOffVariance: this.AutomaticCutOffVariance,
        AutomaticCutOffMinutes: this.AutomaticCutOffMinutes,
      };
      if (sourcefeed) {
        await SourceFeed._SourceRepo.update(payload, {
          where: {
            FeedName: this.FeedName,
          },
          transaction: dbTransaction,
        });
      } else {
        await SourceFeed._SourceRepo.create(payload, {
          transaction: dbTransaction,
        });
      }
    } catch (error) {
      const wrappedError = new ClassError(
        'Feed',
        'FeedErrMsg02',
        error.message,
      );
      await SourceFeed.logger.error({
        error,
        methodName: 'save',
        transaction: dbTransaction,
      });
      throw wrappedError;
    }
  }

  public async getCutOffDetails(dbTransaction: any) {
    try {
      if (this.CurrentCutOffHistoryId) {
        return await FeedCutOffHistory.init(
          this.CurrentCutOffHistoryId,
          dbTransaction,
        );
      }
      return null;
    } catch (error) {
      const wrappedError = new ClassError(
        'Feed',
        'FeedErrMsg03',
        error.message,
      );
      await SourceFeed.logger.error({
        error,
        methodName: 'getCutOffDetails',
        transaction: dbTransaction,
      });
      throw wrappedError;
    }
  }

  public async recordFeedPrice(
    priceData: IPriceData,
    dbTransaction: any,
  ): Promise<FeedHistory> {
    try {
      const feedName = this.FeedName;
      const sourceFeedBuyPrice = priceData.BuyPrice;
      const sourceFeedSellPrice = priceData.SellPrice;

      let FeedBuyPrice: number;
      let FeedSellPrice: number;
      let IsManualCutOffYN = 'N';
      let IsCircuitBreakerActivatedYN = 'N';
      if (this.IsManualPriceActivatedYN === 'Y') {
        const manualPriceHistory = await FeedHistory.getLatestPrice(
          this.FeedName,
          this.FeedName.split('/')[1],
          null,
          dbTransaction,
        );
        FeedBuyPrice = manualPriceHistory.FeedBuyPrice;
        FeedSellPrice = manualPriceHistory.FeedSellPrice;
      } else if (this.IsFeedCutOffYN === 'Y') {
        FeedBuyPrice = 0;
        FeedSellPrice = 0;

        const feedCutOffHistory = await FeedCutOffHistory.init(
          this.CurrentCutOffHistoryId,
          dbTransaction,
        );
        if (feedCutOffHistory.ActivationTrigger === ActivationTrigger.MANUAL) {
          IsManualCutOffYN = 'Y';
        } else {
          IsCircuitBreakerActivatedYN = 'Y';
        }
      } else {
        FeedBuyPrice = priceData.BuyPrice;
        FeedSellPrice = priceData.SellPrice;
      }

      const isSourceFeedAvailableYN = priceData.IsSourceFeedAvailableYN;
      const isManualPriceActivatedYN = this.IsManualPriceActivatedYN;

      let feedHistory = await FeedHistory.init(dbTransaction);
      feedHistory = await feedHistory.saveFeedPrice(
        {
          FeedName: feedName,
          DateTime: new Date(),
          Currency: priceData.Currency,
          SourceFeedBuyPrice: sourceFeedBuyPrice,
          SourceFeedSellPrice: sourceFeedSellPrice,
          FeedBuyPrice: FeedBuyPrice,
          FeedSellPrice: FeedSellPrice,
          IsSourceFeedAvailableYN: isSourceFeedAvailableYN,
          IsCircuitBreakerActivatedYN: IsCircuitBreakerActivatedYN,
          IsManualPriceActivatedYN: isManualPriceActivatedYN,
          IsManualCutOffYN: IsManualCutOffYN,
        },
        dbTransaction,
      );
      // Check Feed Buy and Feed Sell:
      if (FeedBuyPrice !== 0 && FeedSellPrice !== 0) {
        this.Status = 'Available';
      } else {
        this.Status = 'Unavailable';
      }

      await this.save(dbTransaction);
      return feedHistory;
    } catch (error) {
      await SourceFeed.logger.error({
        error,
        methodName: 'recordFeedPrice',
        transaction: dbTransaction,
      });
      throw error;
    }
  }

  private async getPreviousSourceFeedConfig(dbTransaction: any): Promise<
    | {
        AutomaticCutOffVariance: number;
        AutomaticCutOffMinutes: number;
        UpdatedAt: Date;
      }
    | false
  > {
    try {
      const previousActivity = await SourceFeed._ActivityRepo.findOne({
        where: {
          EntityType: 'SourceFeed',
          EntityId: this.FeedName,
          Action: ActionEnum.UPDATE,
          Description: 'Update Circuit Breaker Config for :' + this.FeedName,
        },
        transaction: dbTransaction,
        order: [['PerformedAt', 'DESC']],
      });
      if (previousActivity) {
        const previousConfig: {
          AutomaticCutOffVariance: number;
          AutomaticCutOffMinutes: number;
          FeedName: string;
          Status: string;
          IsSourceFeedAvailableYN: string;
          IsFeedCutOffYN: string;
          CurrentCutOffHistoryId: string;
        } = JSON.parse(previousActivity.EntityValueBefore);
        return {
          AutomaticCutOffVariance: previousConfig.AutomaticCutOffVariance,
          AutomaticCutOffMinutes: previousConfig.AutomaticCutOffMinutes,
          UpdatedAt: previousActivity.PerformedAt,
        };
      }
      return false; // No previous configuration found
    } catch (error) {
      await SourceFeed.logger.error({
        error,
        methodName: 'getPreviousCircuitBreakerConfig',
        transaction: dbTransaction,
      });
      throw error;
    }
  }

  public async circuitBreakerCheck(
    priceData: IPriceData,
    priceSource: PriceSourceBase<SourceFeed>,
    dbTransaction?: any,
  ): Promise<boolean> {
    // Circuit breaker or automatic price cut-off will be activated based on the settings in feed config.
    // The variance check will be applied to the Source Prices from price_PriceFeedHistory table.
    // For example, if the user sets up 5% within 5 minutes, the system will automatically cut off the feed
    // when there is a 5% variance in price compared to the price 5 minutes back. This applies to both Sell and Buy prices.

    try {
      let automaticCutOffVariance = this.AutomaticCutOffVariance;
      let automaticCutOffMinutes = this.AutomaticCutOffMinutes;

      const previousConfig =
        await this.getPreviousSourceFeedConfig(dbTransaction);

      if (previousConfig) {
        const currentTime = new Date().getTime();
        const previousConfigUpdatedTime = previousConfig.UpdatedAt.getTime();
        const currentConfigTimeWindow = automaticCutOffMinutes * 60000;

        const effectiveStartTime = Math.max(
          previousConfigUpdatedTime + currentConfigTimeWindow,
          currentTime,
        );

        if (currentTime < effectiveStartTime) {
          automaticCutOffVariance = previousConfig.AutomaticCutOffVariance;
          automaticCutOffMinutes = previousConfig.AutomaticCutOffMinutes;
        } else {
          automaticCutOffVariance = this.AutomaticCutOffVariance;
          automaticCutOffMinutes = this.AutomaticCutOffMinutes;
        }
      } else {
        automaticCutOffVariance = this.AutomaticCutOffVariance;
        automaticCutOffMinutes = this.AutomaticCutOffMinutes;
      }

      const feedHistories = await SourceFeed._FeedHistoryRepo.findAll({
        where: {
          FeedName: this.FeedName,
          DateTime: {
            [Op.gte]: new Date(
              new Date().getTime() - automaticCutOffMinutes * 60000,
            ),
          },
        },
        order: [['DateTime', 'DESC']],
        transaction: dbTransaction,
      });

      if (
        feedHistories.some(
          ({ IsManualPriceActivatedYN }) => IsManualPriceActivatedYN === 'Y',
        )
      ) {
        return true;
      }

      const validFeedHistories = feedHistories.filter(
        ({ SourceFeedBuyPrice, SourceFeedSellPrice }) =>
          SourceFeedBuyPrice !== 0 && SourceFeedSellPrice !== 0,
      );

      if (validFeedHistories.length === 0) return true;

      const buyPrices = validFeedHistories.map(
        ({ SourceFeedBuyPrice }) => SourceFeedBuyPrice,
      );
      const sellPrices = validFeedHistories.map(
        ({ SourceFeedSellPrice }) => SourceFeedSellPrice,
      );

      buyPrices.push(priceData.BuyPrice);
      sellPrices.push(priceData.SellPrice);

      const [minBuyPrice, maxBuyPrice] = [
        Math.min(...buyPrices),
        Math.max(...buyPrices),
      ];
      const [minSellPrice, maxSellPrice] = [
        Math.min(...sellPrices),
        Math.max(...sellPrices),
      ];

      const buyVariance =
        minBuyPrice > 0 ? ((maxBuyPrice - minBuyPrice) / minBuyPrice) * 100 : 0;
      const sellVariance =
        minSellPrice > 0
          ? ((maxSellPrice - minSellPrice) / minSellPrice) * 100
          : 0;

      // Circuit breaker protection - skip if division by zero or invalid data
      if (
        minBuyPrice <= 0 ||
        minSellPrice <= 0 ||
        !isFinite(buyVariance) ||
        !isFinite(sellVariance) ||
        isNaN(buyVariance) ||
        isNaN(sellVariance)
      ) {
        // Log the issue and allow the price to pass through
        console.warn(
          `Circuit breaker: Invalid price data detected for ${this.FeedName}. Skipping variance check.`,
        );
        return true; // Allow price to pass, retry at next interval
      }

      if (
        (buyVariance > automaticCutOffVariance ||
          sellVariance > automaticCutOffVariance) &&
        this.IsFeedCutOffYN === 'N'
      ) {
        await this.cutOff(priceSource, dbTransaction);
        return false;
      }

      return true;
    } catch (error) {
      await SourceFeed.logger.error({
        error,
        methodName: 'circuitBreakerCheck',
        transaction: dbTransaction,
      });
      throw error;
    }
  }

  public async cutOff(
    priceSource: PriceSourceBase<SourceFeed>,
    dbTransaction: any,
    activatedById?: string,
  ) {
    try {
      let feedCutOffHistory = await FeedCutOffHistory.init();

      feedCutOffHistory.FeedName = this.FeedName;
      let activatedBy = null;
      let isManualCutOff = 'N';
      let isCircuitBreakerActivated = 'N';
      if (activatedById) {
        const user = await LoginUser.init(null, parseInt(activatedById));
        activatedBy = user.FullName;
        feedCutOffHistory.ActivationTrigger = ActivationTrigger.MANUAL;
        isManualCutOff = 'Y';
      } else {
        feedCutOffHistory.ActivationTrigger = ActivationTrigger.LIMIT_BREACH;
        isCircuitBreakerActivated = 'Y';
      }

      feedCutOffHistory = await feedCutOffHistory.create(
        dbTransaction,
        activatedById,
      );

      let feedHistory = await FeedHistory.init(dbTransaction);
      const fetchResult = await priceSource.fetchPrice({
        [this.FeedName]: this,
      });
      const priceData = fetchResult[this.FeedName];
      feedHistory = await feedHistory.saveFeedPrice(
        {
          FeedName: this.FeedName,
          DateTime: new Date(),
          Currency: priceData.Currency,
          SourceFeedBuyPrice: priceData.BuyPrice,
          SourceFeedSellPrice: priceData.SellPrice,
          FeedBuyPrice: 0,
          FeedSellPrice: 0,
          IsSourceFeedAvailableYN: priceData.IsSourceFeedAvailableYN,
          IsManualCutOffYN: isManualCutOff,
          IsCircuitBreakerActivatedYN: isCircuitBreakerActivated,
          IsManualPriceActivatedYN: 'N',
        },
        dbTransaction,
      );

      await this.createTomeiPriceHistory(feedHistory, dbTransaction);

      this.IsFeedCutOffYN = 'Y';
      this.CurrentCutOffHistoryId = feedCutOffHistory.FeedCutOffHistoryId;
      await this.save(dbTransaction);
    } catch (error) {
      await SourceFeed.logger.error({
        error,
        methodName: 'cutOff',
        transaction: dbTransaction,
      });
      throw error;
    }
  }

  public async deactivateCutOff(
    priceSource: PriceSourceBase<SourceFeed>,
    dbTransaction: any,
    deactivatedById: string,
  ) {
    try {
      await FeedCutOffHistory.deactivate(
        this.FeedName,
        deactivatedById,
        dbTransaction,
      );

      this.IsFeedCutOffYN = 'N';
      await this.save(dbTransaction);

      const fetchResult = await priceSource.fetchPrice({
        [this.FeedName]: this,
      });
      const priceData = fetchResult[this.FeedName];

      const feedHistory = await this.recordFeedPrice(priceData, dbTransaction);
      await this.createTomeiPriceHistory(feedHistory, dbTransaction);
    } catch (error) {
      await SourceFeed.logger.error({
        error,
        methodName: 'deactivateCutOff',
        transaction: dbTransaction,
      });
      throw error;
    }
  }

  public async updateCircuitBreakerConfig(
    AutomaticCutOffVariance: number,
    AutomaticCutOffMinutes: number,
    dbTransaction: any,
    loginUser: LoginUser,
  ) {
    try {
      const EntityValueBefore = {
        AutomaticCutOffVariance: this.AutomaticCutOffVariance,
        AutomaticCutOffMinutes: this.AutomaticCutOffMinutes,
        FeedName: this.FeedName,
        Status: this.Status,
        IsSourceFeedAvailableYN: this.IsSourceFeedAvailableYN,
        IsFeedCutOffYN: this.IsFeedCutOffYN,
        CurrentCutOffHistoryId: this.CurrentCutOffHistoryId,
      };

      this.AutomaticCutOffVariance = AutomaticCutOffVariance;
      this.AutomaticCutOffMinutes = AutomaticCutOffMinutes;

      await SourceFeed._SourceRepo.update(
        {
          AutomaticCutOffVariance: this.AutomaticCutOffVariance,
          AutomaticCutOffMinutes: this.AutomaticCutOffMinutes,
        },
        {
          where: {
            FeedName: this.FeedName,
          },
          transaction: dbTransaction,
        },
      );

      // Log the activity history for updating the circuit breaker configuration
      const activity = new Activity();
      activity.ActivityId = activity.createId();
      activity.Description =
        'Update Circuit Breaker Config for :' + this.FeedName;
      activity.Action = ActionEnum.UPDATE;
      activity.EntityType = 'SourceFeed';
      activity.EntityId = this.FeedName;
      activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
      activity.EntityValueAfter = JSON.stringify({
        AutomaticCutOffVariance: this.AutomaticCutOffVariance,
        AutomaticCutOffMinutes: this.AutomaticCutOffMinutes,
        FeedName: this.FeedName,
        Status: this.Status,
        IsSourceFeedAvailableYN: this.IsSourceFeedAvailableYN,
        IsFeedCutOffYN: this.IsFeedCutOffYN,
        CurrentCutOffHistoryId: this.CurrentCutOffHistoryId,
      });
      await activity.create(loginUser.ObjectId, dbTransaction);
    } catch (error) {
      await SourceFeed.logger.error({
        error,
        methodName: 'updateCircuitBreakerConfig',
        transaction: dbTransaction,
      });
      throw error;
    }
  }

  public static async getAllSourceFeed(
    dbTransaction: any,
  ): Promise<SourceFeed[]> {
    try {
      const sourceFeeds = await SourceFeed._SourceRepo.findAll({
        transaction: dbTransaction,
      });
      return sourceFeeds.map((sourceFeed) => new SourceFeed(sourceFeed));
    } catch (error) {
      await SourceFeed.logger.error({
        error,
        methodName: 'getAllSourceFeed',
        transaction: dbTransaction,
      });
      throw error;
    }
  }
}
