import csv

from wagtail.blocks import StreamValue
from books.models.contentblock import ContentBlock
from books.models.custom_document import CustomDocument

# from books.schema.nodes.content import ContentBlockNode
from graphql_relay import to_global_id

"""
Script with helpers to make a CSV export of old document links to compare them to new document links
"""

columns = [
    "content_block_id",
    "document_id",
    "old",
    "old_s3",
    "new",
    "module",
    "content_block",
]


def format_link_s3(document):
    return (
        f"https://skillbox-my-kv-prod.s3-eu-west-1.amazonaws.com{document.file.url[6:]}"
    )


def format_link_django(document):
    return f"https://app.mykv.ch{document.url}"


def handle_all_content_blocks():
    with open("source.csv", mode="w", encoding="utf-8") as file:
        writer = csv.DictWriter(file, fieldnames=columns)
        writer.writeheader()
        # print("id,old link,new link,module,content block link")
        for cb in ContentBlock.objects.all():
            handle_content_block(content_block=cb, contents=cb.contents, writer=writer)


def handle_even_older_backup():  # helper to get backup from february and may 2023 and compare the old link with the old link from october
    output_columns = columns + ["older", "older_s3"]
    with open("old-links.csv", mode="r", encoding="utf-8") as file:
        with open("even-older-links.csv", mode="w", encoding="utf-8") as destination:
            reader = csv.DictReader(file)
            writer = csv.DictWriter(destination, fieldnames=output_columns)
            writer.writeheader()

            for row in reader:
                try:
                    document_id = row["document_id"]
                    if document_id == "":
                        continue
                    document = CustomDocument.objects.get(id=document_id)
                    old_s3_url = format_link_s3(document)
                    old_django_url = format_link_django(document)

                    if row["old_s3"] == old_s3_url:
                        continue
                    row["older"] = old_django_url
                    row["older_s3"] = old_s3_url

                    writer.writerow(row)
                except CustomDocument.DoesNotExist:
                    continue


def fill_csv_with_old_data():
    with open("source.csv", mode="r", encoding="utf-8") as file:
        with open("output.csv", mode="w", encoding="utf-8") as destination:
            reader = csv.DictReader(file)
            writer = csv.DictWriter(destination, fieldnames=columns)
            writer.writeheader()

            for row in reader:
                try:
                    document = CustomDocument.objects.get(id=row["document_id"])
                    old_django_url = format_link_django(document)
                    old_s3_url = format_link_s3(document)
                    # print(document.file.url)
                    # print(row["new"])
                    row["old"] = old_django_url
                    row["old_s3"] = old_s3_url
                except ValueError:
                    pass
                except CustomDocument.DoesNotExist:
                    print(f"Document with id {row['document_id']} does not exist")

                writer.writerow(row)


def print_document(document, content_block, writer):
    content_block_id = content_block.id
    try:
        # new_link = f"https://skillbox-my-kv-prod.s3-eu-west-1.amazonaws.com{document.file.url[6:]}"
        new_link = format_link_django(document)
        document_id = document.id
    except AttributeError:
        new_link = "missing document"
        document_id = ""
    module_slug = content_block.get_parent().get_parent().slug
    module_url = f"https://app.mykv.ch/module/{module_slug}"
    # content_block_node = ContentBlockNode(content_block)
    # content_block_path = content_block_node.resolve_path(content_block, None)
    node_id = to_global_id("ContentBlockNode", content_block.id)
    # content_block_url = f"https://app.mykv.ch/{content_block_path}"
    content_block_url = f"https://app.mykv.ch/content/{node_id}"
    # print(f"{id},,{new_link},{module_url}")
    row = {
        "content_block_id": content_block_id,
        "document_id": document_id,
        "old": "",
        "old_s3": "",
        "new": new_link,
        "module": module_url,
        "content_block": content_block_url,
    }
    writer.writerow(row)
    # writer.writerow(
    #     [content_block_id, document_id, "", new_link, module_url, content_block_url]
    # )


def handle_content_block(content_block: ContentBlock, contents: StreamValue, writer):
    # contents: StreamValue
    document_blocks = contents.blocks_by_name("cms_document_block")
    instruction_block = contents.blocks_by_name("instruction")
    solution_block = contents.blocks_by_name("solution")
    list_item_blocks = contents.blocks_by_name("content_list_item")
    for li in list_item_blocks:
        handle_content_block(
            contents=li.value, content_block=content_block, writer=writer
        )
    for db in document_blocks:
        document = db.value
        print_document(document, content_block, writer)
    for ib in instruction_block:
        document = ib.value["document"]
        print_document(document, content_block, writer)
    for sb in solution_block:
        document = sb.value["document"]
        print_document(document, content_block, writer)
