// services/length-strategy.service.ts
import { Injectable } from '@nestjs/common';
import { ContentStyle } from '../enum/style.enum';
import { ContentTone } from '../enum/tone.enum';
import { AccountTier } from '../enum/account-tier.enum';
import { PostLength } from '../enum/post-length.enum';
import { Platform } from '../enum/platform.enum';
import {LENGTH_RANGES, LengthRange, PLATFORM_LIMITS} from "../config/platform-limits";

export interface LengthDecision {
    postLength: PostLength;
    range: LengthRange;
    hardLimit: number;
    reason: string;
}

@Injectable()
export class LengthStrategyService {
    /**
     * Quyết định độ dài tối ưu dựa trên:
     * 1. User request (nếu có)
     * 2. Content style + tone
     * 3. Account tier
     * 4. Platform
     */
    decide(params: {
        platform: Platform;
        tier: AccountTier;
        style: ContentStyle;
        tone: ContentTone;
        requestedLength?: PostLength; // user override
    }): LengthDecision {
        const { platform, tier, style, tone, requestedLength } = params;
        const hardLimit = PLATFORM_LIMITS[platform][tier];

        // 1. User explicit request -> tôn trọng
        if (requestedLength) {
            return {
                postLength: requestedLength,
                range: this.capRange(LENGTH_RANGES[requestedLength], hardLimit),
                hardLimit,
                reason: `User requested: ${requestedLength}`,
            };
        }

        // 2. Free account -> luôn SHORT
        if (tier === AccountTier.FREE && platform === Platform.X) {
            return {
                postLength: PostLength.SHORT,
                range: LENGTH_RANGES[PostLength.SHORT],
                hardLimit: 280,
                reason: 'Free account on X: max 280 chars',
            };
        }

        // 3. Smart default theo style (Premium/Plus)
        const styleDefault = this.styleBasedLength(style, tone);

        return {
            postLength: styleDefault,
            range: this.capRange(LENGTH_RANGES[styleDefault], hardLimit),
            hardLimit,
            reason: `${tier} + ${style}/${tone}: ${styleDefault}`,
        };
    }

    /**
     * Core logic: style + tone -> length.
     * Dựa trên best practices của X Premium 2025-2026.
     */
    private styleBasedLength(style: ContentStyle, tone: ContentTone): PostLength {
        // tone urgent -> short
        if(tone === ContentTone.URGENT) {
            return PostLength.SHORT;
        }
        // Breaking news KHẨN CẤP -> vẫn ngắn dù có Premium
        // (vì cần viral tốc độ, retweet nhanh)
        // @ts-ignore
        // if (style === ContentStyle.BREAKING_NEWS && tone === ContentTone.URGENT) {
        //     return PostLength.SHORT;
        // }

        // Breaking news non-urgent -> MEDIUM (có context)
        if (style === ContentStyle.BREAKING_NEWS) {
            return PostLength.MEDIUM;
        }

        // Meme / Humorous -> luôn SHORT
        if (style === ContentStyle.MEME || tone === ContentTone.HUMOROUS) {
            return PostLength.SHORT;
        }

        // Educational / Tech / Finance -> LONG (Premium sweet spot)
        if (
            style === ContentStyle.EDUCATIONAL ||
            style === ContentStyle.TECH ||
            style === ContentStyle.FINANCE
        ) {
            return PostLength.LONG;
        }

        // Crypto analysis -> MEDIUM to LONG
        if (style === ContentStyle.CRYPTO) {
            return tone === ContentTone.HYPE ? PostLength.SHORT : PostLength.MEDIUM;
        }

        // Lifestyle -> MEDIUM
        if (style === ContentStyle.LIFESTYLE) {
            return PostLength.MEDIUM;
        }

        // Default
        return PostLength.MEDIUM;
    }

    /**
     * Cap range trong hard limit (đảm bảo không vượt platform limit).
     */
    private capRange(range: LengthRange, hardLimit: number): LengthRange {
        return {
            min: Math.min(range.min, hardLimit),
            max: Math.min(range.max, hardLimit),
            sweet: Math.min(range.sweet, hardLimit),
        };
    }
}
