import csv
import json
import os
import shutil

import requests
from tqdm import tqdm

from ..utils.loc_utils import get_country_and_city

COUNTRIES_PDC = "https://api.hungermapdata.org/v2/pdc.json"
SRC = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def parse_pdc():
    """
    Parse the pdc data from the API and return a csv file.
    The csv file will contain the following columns:
    - id
    - type
    - severityId
    - hazardName
    - latitude
    - longitude
    - createDate
    - lastUpdate
    """

    # Clean the existing files
    if not os.path.exists(os.path.join(SRC, "assets", "pdc_data")):
        os.makedirs(os.path.join(SRC, "assets", "pdc_data"))
    elif os.path.exists(os.path.join(SRC, "assets", "pdc_data", "pdc.csv")):
        print("Removing the existing pdc.csv file.")
        os.remove(os.path.join(SRC, "assets", "pdc_data", "pdc.csv"))
    if os.path.exists(os.path.join(SRC, "assets", "pdc_data", "pdc.json")):
        print("Removing the existing pdc.json file.")
        os.remove(os.path.join(SRC, "assets", "pdc_data", "pdc.json"))

    # Prepare new csv file
    csv_file = open(
        os.path.join(SRC, "assets", "pdc_data", "pdc.csv"), "w", encoding="utf-8"
    )
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(
        [
            "id",
            "type",
            "severityId",
            "hazardName",
            "latitude",
            "longitude",
            "createDate",
            "lastUpdate",
        ]
    )

    # Get the data from the API
    response = requests.get(COUNTRIES_PDC).json()
    with open(
        os.path.join(SRC, "assets", "pdc_data", "pdc.json"), "w", encoding="utf-8"
    ) as json_file:
        json.dump(response, json_file)
    response = response["body"]

    # Write the data to the csv file
    for feature in response:
        id = feature["Id"]
        type = feature["type"]
        severity_id = feature["severity_id"]
        hazard_name = feature["hazard_name"]
        latitude = feature["latitude"]
        longitude = feature["longitude"]
        create_date = feature["create_date"]
        last_update = feature["last_update"]
        csv_writer.writerow(
            [
                id,
                type,
                severity_id,
                hazard_name,
                latitude,
                longitude,
                create_date,
                last_update,
            ]
        )
    csv_file.close()


def parse_pdc_per_country(csv_file_path):
    """
    This function preprocesses the csv-file coming from `parse_pdc` function.
    It groups the data by country and writes the data to a new csv file.
    A file is created for each country with the following columns:
    -- id
    -- type
    -- severityId
    -- hazardName
    -- latitude
    -- longitude
    -- createDate
    -- lastUpdate

    Args:
        csv_file_path (_type_): The path to the csv file containing the pdc data.
    """
    if os.path.exists(os.path.join(SRC, "assets", "pdc_data", "countries")):
        print("Removing the existing countries folder.")
        shutil.rmtree(os.path.join(SRC, "assets", "pdc_data", "countries"))
    os.makedirs(os.path.join(SRC, "assets", "pdc_data", "countries"))
    file = open(csv_file_path, "r", encoding="utf-8")

    # Iterate over the rows and group the data by country using `get_country_and_city` function
    country_data = {}

    # get line count from file
    line_count = 0
    for line in file:
        line_count += 1

    # reset file pointer
    file.seek(0)
    csv_reader = csv.reader(file)
    next(csv_reader)  # Skip the header
    for row in tqdm(csv_reader, desc="Processing PDC data", total=line_count):
        if len(row) < 8:
            print(f"Skipping row with insufficient columns: {row}")
            continue
        latitude = row[4]
        longitude = row[5]
        country, city = get_country_and_city(latitude, longitude)
        if country is None:
            country = "Not specified area"
        if country not in country_data:
            country_data[country] = []
        # Add city to the data
        country_data[country].append(
            [
                row[0],
                row[1],
                row[2],
                row[3],
                row[4],
                row[5],
                row[6],
                row[7],
            ]
        )

    # Write the data to the csv files
    for country, data in country_data.items():
        csv_file = open(
            os.path.join(SRC, "assets", "pdc_data", "countries", f"{country}.csv"),
            "w",
            encoding="utf-8",
        )
        csv_writer = csv.writer(csv_file)
        csv_writer.writerow(
            [
                "id",
                "type",
                "severityId",
                "hazardName",
                "latitude",
                "longitude",
                "createDate",
                "lastUpdate",
            ]
        )
        for row in data:
            csv_writer.writerow(row)
        csv_file.close()


def main():
    parse_pdc()
    parse_pdc_per_country(os.path.join(SRC, "assets", "pdc_data", "pdc.csv"))


if __name__ == "__main__":
    main()

Create a test file for this parser