from app.sample_models import SpreadsheetModel, SpreadsheetResponse
from fastapi import APIRouter, UploadFile, File, HTTPException
import logging
from app.services.spreadsheet_service import (
    SampleSpreadsheetImporter,
    SpreadsheetImportError,
)
from fastapi.responses import FileResponse
import os
from pydantic import ValidationError  # Import ValidationError here
from app.row_storage import row_storage  # Import the RowStorage instance

router = APIRouter()
logger = logging.getLogger(__name__)

importer = (
    SampleSpreadsheetImporter()
)  # assuming this is a singleton or manageable instance


@router.get("/download-template", response_class=FileResponse)
async def download_template():
    """Serve a template file for spreadsheet upload."""
    current_dir = os.path.dirname(__file__)
    template_path = os.path.join(
        current_dir, "../../downloads/V7_TELLSamplesSpreadsheetTemplate.xlsx"
    )

    if not os.path.exists(template_path):
        raise HTTPException(status_code=404, detail="Template file not found.")

    return FileResponse(
        template_path,
        filename="template.xlsx",
        media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )


@router.post("/upload", response_model=SpreadsheetResponse)
async def upload_file(file: UploadFile = File(...)):
    """Process the uploaded spreadsheet and return validation results."""
    try:
        logger.info(f"Received file: {file.filename}")

        # Validate file format
        if not file.filename.endswith(".xlsx"):
            logger.error("Invalid file format")
            raise HTTPException(
                status_code=400,
                detail="Invalid file format. Please upload an .xlsx file.",
            )

        # Initialize the importer and process the spreadsheet
        (
            validated_model,
            errors,
            raw_data,
            headers,
        ) = importer.import_spreadsheet_with_errors(file)

        # Extract unique values for dewars, pucks, and samples
        dewars = {sample.dewarname for sample in validated_model if sample.dewarname}
        pucks = {sample.puckname for sample in validated_model if sample.puckname}
        samples = {
            sample.crystalname for sample in validated_model if sample.crystalname
        }

        # Construct the response model with the processed data
        response_data = SpreadsheetResponse(
            data=validated_model,
            errors=errors,
            raw_data=raw_data,
            dewars_count=len(dewars),
            dewars=list(dewars),
            pucks_count=len(pucks),
            pucks=list(pucks),
            samples_count=len(samples),
            samples=list(samples),
            headers=headers,  # Include headers in the response
        )

        # Store row data for future use
        for idx, row in enumerate(validated_model):
            row_num = idx + 4  # Adjust row numbering if necessary
            row_storage.set_row(row_num, row.dict())

        logger.info(
            f"Returning response with {len(validated_model)}"
            f"records and {len(errors)} errors."
        )
        return response_data

    except SpreadsheetImportError as e:
        logger.error(f"Spreadsheet import error: {str(e)}")
        raise HTTPException(
            status_code=400, detail=f"Error processing spreadsheet: {str(e)}"
        )

    except Exception as e:
        logger.error(f"Unexpected error occurred: {str(e)}")
        raise HTTPException(
            status_code=500,
            detail=f"Failed to upload file. Please try again. Error: {str(e)}",
        )


@router.post("/validate-cell")
async def validate_cell(data: dict):
    row_num = data.get("row")
    col_name = data.get("column")
    value = data.get("value")

    # Get the full data for the row
    current_row_data = row_storage.get_row(row_num)

    # Update the cell value
    current_row_data[col_name] = importer._clean_value(
        value, importer.get_expected_type(col_name)
    )

    # Temporarily store the updated row data
    row_storage.set_row(row_num, current_row_data)

    logger.info(f"Validating cell: row {row_num}, column {col_name}, value {value}")

    try:
        # Ensure we're using the full row data context for validation
        SpreadsheetModel(
            **current_row_data
        )  # Instantiates the Pydantic model, performing validation
        logger.info(f"Validation succeeded for row {row_num}, column {col_name}")
        return {"is_valid": True, "message": ""}
    except ValidationError as e:
        # Extract the first error message
        message = e.errors()[0]["msg"]
        logger.error(
            f"Validation failed for row {row_num}, column {col_name}: {message}"
        )
        return {"is_valid": False, "message": message}
