#!/usr/bin/env python3

"""
Expand a SNP list with LD neighbors from pre-computed magenpy LD matrices.
"""

import argparse
import logging
import os
import sys

import numpy as np
import pandas as pd

import magenpy as mgp
from magenpy.parsers.misc_parsers import read_snp_filter_file
from magenpy.utils.bin_utils import (
    format_cli_block,
    format_rows,
    format_sections,
    print_cli_logo,
    setup_cli_logger,
)

logger = logging.getLogger("magenpy.cli.mgp_expand_ld")

LOGO_DESCRIPTION = "< Find tagging variants using pre-computed LD matrices >"


def format_run_summary(args, n_focal_snps=None):
    """
    Build a compact summary of the requested LD expansion operation.

    :param args: Parsed command-line arguments.
    :param n_focal_snps: Number of unique focal SNPs read from the input file.
    :return: A formatted multi-line string.
    """
    sections = [
        ("LD source", [("LD path", args.ld_path)]),
        (
            "Variant input",
            [
                ("SNP file", args.snp_file),
                ("SNP column", args.snp_column),
                ("Unique focal SNPs", n_focal_snps),
            ],
        ),
        (
            "Expansion",
            [
                ("R2 threshold", args.r2_threshold),
                ("Correlation threshold", round(float(np.sqrt(args.r2_threshold)), 2)),
                ("Include LD relationships", args.include_ld),
            ],
        ),
        ("Output", [("Output file", args.output_file)]),
    ]

    return format_sections(sections)


def read_snp_file(path, column=0):
    """
    Read a plink-style SNP filter file.
    """
    return (
        pd.Series(read_snp_filter_file(path, snp_id_col=column))
        .dropna()
        .astype(str)
        .tolist()
    )


def unique_preserving_order(values):
    """
    Remove duplicates while preserving input order.
    """
    return list(dict.fromkeys(values))


def make_data_loader(args):
    """
    Load LD matrices through GWADataLoader.
    """
    gdl = mgp.GWADataLoader(ld_store_files=args.ld_path)
    if gdl.ld is None or len(gdl.ld) < 1:
        raise FileNotFoundError(f"No LD matrices were found at: {args.ld_path}")
    return gdl


def focal_indices_by_chromosome(gdl, focal_snps):
    """
    Find focal SNP indices in each LD matrix with vectorized hash lookups.

    :param gdl: A ``GWADataLoader`` object with LD matrices.
    :param focal_snps: Ordered focal SNP IDs supplied by the user.
    :return: Dictionary mapping chromosomes to matched LD-matrix indices.
    """
    focal_snps = np.asarray(focal_snps, dtype=str)
    matched = {}
    for chrom, ld_mat in gdl.ld.items():
        indices = pd.Index(ld_mat.snps.astype(str)).get_indexer(focal_snps)
        indices = indices[indices >= 0].astype(np.int32, copy=False)
        if indices.size > 0:
            matched[chrom] = indices
    return matched


def expand_chromosome(ld_mat, focal_indices, corr_threshold):
    """
    Return SNPs tagged by focal SNPs in one LD matrix.
    """
    if focal_indices.size < 1:
        return []

    return ld_mat.find_tagging_variants(
        focal_indices,
        threshold=corr_threshold,
        return_value="snps",
    ).astype(str).tolist()


def extract_ld_relationships(ld_mat, focal_indices, corr_threshold):
    """Extract focal-to-neighbor LD relationships with one sparse matrix load."""
    ld_snps = ld_mat.snps.astype(str)
    if focal_indices.size < 1:
        return []

    expanded_indices = ld_mat.find_tagging_variants(
        focal_indices,
        threshold=corr_threshold,
        return_value="index",
    )
    expanded_snps = ld_snps[expanded_indices]
    focal_positions = np.searchsorted(expanded_indices, focal_indices)

    ld_mat.set_mask(expanded_indices)
    try:
        expanded_ld = ld_mat.to_csr(
            return_symmetric=True,
            dtype=np.float32,
        )
    finally:
        ld_mat.reset_mask()

    focal_ld = expanded_ld[focal_positions].tocoo()
    original_snps = ld_snps[focal_indices][focal_ld.row]
    neighbor_snps = expanded_snps[focal_ld.col]
    correlations = focal_ld.data.astype(float, copy=False)
    keep = (original_snps != neighbor_snps) & (
        np.abs(correlations) >= corr_threshold
    )

    return [
        {
            "Original_SNP": original_snp,
            "SNP": neighbor_snp,
            "R": correlation,
        }
        for original_snp, neighbor_snp, correlation in zip(
            original_snps[keep],
            neighbor_snps[keep],
            correlations[keep],
        )
    ]


def write_variants(snps, output_file):
    """
    Write expanded SNP IDs to a tab-delimited output table.
    """
    os.makedirs(os.path.dirname(os.path.abspath(output_file)), exist_ok=True)
    pd.DataFrame({"SNP": snps}).to_csv(output_file, sep="\t", index=False)


def write_ld_relationships(relationships, output_file):
    """Write focal variants, their neighbors, and pairwise LD values."""
    os.makedirs(os.path.dirname(os.path.abspath(output_file)), exist_ok=True)
    pd.DataFrame(
        relationships,
        columns=["Original_SNP", "SNP", "R"],
    ).to_csv(output_file, sep="\t", index=False)


def parse_args():
    parser = argparse.ArgumentParser(
        description="Expand a SNP list with LD neighbors from pre-computed magenpy LD matrices."
    )
    parser.add_argument(
        "-l", "--ld", dest="ld_path", required=True, help="LD matrix path or glob."
    )
    parser.add_argument(
        "--snp-file", required=True, help="Plink-style SNP filter file."
    )
    parser.add_argument(
        "--snp-column", type=int, default=0, help="Zero-based SNP column index."
    )
    parser.add_argument("--r2-threshold", type=float, default=0.1, help="Default: 0.1.")
    parser.add_argument(
        "--include-ld",
        action="store_true",
        help="Write original-to-neighbor relationships with their R values.",
    )
    parser.add_argument("--output-file", required=True, help="Output table path.")
    parser.add_argument(
        "--log-level",
        default="WARNING",
        choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
        help="Logging level.",
    )
    return parser.parse_args()


def main():
    """
    Parse CLI inputs, expand focal SNPs to LD neighbors, and write SNP IDs.
    """
    args = parse_args()
    setup_cli_logger(logger, args.log_level)
    print_cli_logo(mgp, LOGO_DESCRIPTION, output_logger=logger)

    if not 0.0 < args.r2_threshold <= 1.0:
        raise ValueError("--r2-threshold must be in (0, 1].")

    focal_snps = unique_preserving_order(read_snp_file(args.snp_file, args.snp_column))
    logger.info(
        "%s",
        format_cli_block(
            "LD expansion request",
            format_run_summary(args, n_focal_snps=len(focal_snps)),
        ),
    )
    gdl = make_data_loader(args)
    focal_indices = focal_indices_by_chromosome(gdl, focal_snps)
    n_matched_focal = sum(len(indices) for indices in focal_indices.values())
    logger.info(
        "Matched %s of %s focal variants to %s LD matrices before expansion.",
        n_matched_focal,
        len(focal_snps),
        len(focal_indices),
    )
    corr_threshold = float(np.sqrt(args.r2_threshold))

    if args.include_ld:
        relationships = []
        for chrom, ld_mat in sorted(gdl.ld.items(), key=lambda x: str(x[0])):
            relationships.extend(
                extract_ld_relationships(
                    ld_mat,
                    focal_indices.get(chrom, np.empty(0, dtype=np.int32)),
                    corr_threshold,
                )
            )
            ld_mat.release()
        write_ld_relationships(relationships, args.output_file)
        n_output = len(relationships)
        output_label = "Output relationships"
    else:
        expanded = list(focal_snps)
        for chrom, ld_mat in sorted(gdl.ld.items(), key=lambda x: str(x[0])):
            expanded.extend(
                expand_chromosome(
                    ld_mat,
                    focal_indices.get(chrom, np.empty(0, dtype=np.int32)),
                    corr_threshold,
                )
            )
            ld_mat.release()

        output_snps = unique_preserving_order(expanded)
        write_variants(output_snps, args.output_file)
        n_output = len(output_snps)
        output_label = "Output variants"

    logger.info(
        "LD expansion completed.\n%s",
        format_rows(
            [
                ("Input variants", len(focal_snps)),
                (output_label, n_output),
                ("Output file", args.output_file),
            ]
        ),
    )


if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        logger.error("Error: %s", e)
        sys.exit(1)
