from fastapi import APIRouter, HTTPException
from app.salary_analytics.helpers.response_helpers import AnalysisResponse
from app.salary_analytics.helpers.data_checks import check_data_loaded
from app.utils.logger import logger
import time
from app.salary_analytics.core.state import state

router = APIRouter()


@router.post("/keyword", response_model=AnalysisResponse)
async def analyze_keyword():
    """Run keyword-based salary transaction analysis."""
    start_time = time.time()
    try:
        check_data_loaded()
        logger.info("Starting keyword analysis...")
        data = state.pipeline.run_keyword_analysis()
        logger.info(f"Keyword analysis completed. Found {len(data)} matches")
        response = AnalysisResponse(
            message="Keyword analysis completed successfully",
            data={"count": len(data)}
        )
        logger.info(f"Keyword analysis endpoint completed in {time.time() - start_time:.2f} seconds")
        return response
    except Exception as e:
        logger.error(f"Error in keyword analysis: {str(e)}")
        logger.info(f"Keyword analysis endpoint failed after {time.time() - start_time:.2f} seconds")
        raise HTTPException(status_code=500, detail=str(e))
 


@router.post("/consistent-amount", response_model=AnalysisResponse)
async def analyze_consistent_amount():
    """Run consistent amount transaction analysis."""
    start_time = time.time()
    try:
        check_data_loaded()
        logger.info("Starting consistent amount analysis...")
        data = state.pipeline.run_consistent_amount_analysis()
        logger.info(f"Consistent amount analysis completed. Found {len(data)} matches")
        response = AnalysisResponse(
            message="Consistent amount analysis completed successfully",
            data={"count": len(data)}
        )
        logger.info(f"Consistent amount analysis endpoint completed in {time.time() - start_time:.2f} seconds")
        return response
    except Exception as e:
        logger.error(f"Error in consistent amount analysis: {str(e)}")
        logger.info(f"Consistent amount analysis endpoint failed after {time.time() - start_time:.2f} seconds")
        raise HTTPException(status_code=500, detail=str(e))




@router.post("/transaction-type", response_model=AnalysisResponse)
async def analyze_transaction_type():
    """Run transaction type analysis."""
    start_time = time.time()
    try:
        check_data_loaded()
        logger.info("Starting transaction type analysis...")
        data = state.pipeline.run_transaction_type_analysis()
        logger.info(f"Transaction type analysis completed. Found {len(data)} matches")
        response = AnalysisResponse(
            message="Transaction type analysis completed successfully",
            data={"count": len(data)}
        )
        logger.info(f"Transaction type analysis endpoint completed in {time.time() - start_time:.2f} seconds")
        return response
    except Exception as e:
        logger.error(f"Error in transaction type analysis: {str(e)}")
        logger.info(f"Transaction type analysis endpoint failed after {time.time() - start_time:.2f} seconds")
        raise HTTPException(status_code=500, detail=str(e))

