# Copyright (C) 2025 Leiden University Medical Center
# This file is part of cram-archiver
#
# cram-archiver is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# cram-archiver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with cram-archiver. If not, see <https://www.gnu.org/licenses/

from pathlib import Path

from cram_archiver.references import ReferenceID

import pytest

TEST_DATA = Path(__file__).parent / "data"


TEST_REFERENCE_ID_STRING = """chrM	16571
chr1	249250621
chr2	243199373
chr3	198022430
chr4	191154276
chr5	180915260
chr6	171115067
chr7	159138663
chr8	146364022
chr9	141213431
chr10	135534747
chr11	135006516
chr12	133851895
chr13	115169878
chr14	107349540
chr15	102531392
chr16	90354753
chr17	81195210
chr18	78077248
chr19	59128983
chr20	63025520
chr21	48129895
chr22	51304566
chrX	155270560
chrY	59373566
chr1_gl000191_random	106433
chr1_gl000192_random	547496
chr4_gl000193_random	189789
chr4_gl000194_random	191469
chr7_gl000195_random	182896
chr8_gl000196_random	38914
chr8_gl000197_random	37175
chr9_gl000198_random	90085
chr9_gl000199_random	169874
chr9_gl000200_random	187035
chr9_gl000201_random	36148
chr11_gl000202_random	40103
chr17_gl000203_random	37498
chr17_gl000204_random	81310
chr17_gl000205_random	174588
chr17_gl000206_random	41001
chr18_gl000207_random	4262
chr19_gl000208_random	92689
chr19_gl000209_random	159169
chr21_gl000210_random	27682
chrUn_gl000211	166566
chrUn_gl000212	186858
chrUn_gl000213	164239
chrUn_gl000214	137718
chrUn_gl000215	172545
chrUn_gl000216	172294
chrUn_gl000217	172149
chrUn_gl000218	161147
chrUn_gl000219	179198
chrUn_gl000220	161802
chrUn_gl000221	155397
chrUn_gl000222	186861
chrUn_gl000223	180455
chrUn_gl000224	179693
chrUn_gl000225	211173
chrUn_gl000226	15008
chrUn_gl000227	128374
chrUn_gl000228	129120
chrUn_gl000229	19913
chrUn_gl000230	43691
chrUn_gl000231	27386
chrUn_gl000232	40652
chrUn_gl000233	45941
chrUn_gl000234	40531
chrUn_gl000235	34474
chrUn_gl000236	41934
chrUn_gl000237	45867
chrUn_gl000238	39939
chrUn_gl000239	33824
chrUn_gl000240	41933
chrUn_gl000241	42152
chrUn_gl000242	43523
chrUn_gl000243	43341
chrUn_gl000244	39929
chrUn_gl000245	36651
chrUn_gl000246	38154
chrUn_gl000247	36422
chrUn_gl000248	39786
chrUn_gl000249	38502
"""


def test_reference_id_init():
    ref = ReferenceID(TEST_REFERENCE_ID_STRING)
    assert ref.id == TEST_REFERENCE_ID_STRING


@pytest.mark.parametrize("file", [
    TEST_DATA / "empty.cram",
    TEST_DATA / "empty.bam",
    TEST_DATA / "ucsc.hg19_nohap.fasta.fai",
])
def test_reference_id_from_file(file):
    assert ReferenceID.from_file(str(file)) == ReferenceID(TEST_REFERENCE_ID_STRING)


def test_reference_id_mt():
    assert (ReferenceID.from_file(str(TEST_DATA / "GM24385_1.bam")) ==
            ReferenceID.from_file(str(TEST_DATA / "NC012920.1.fasta.fai")))
