import os
from api import AppleMusic
import unicodedata

def check_explicitness(album_link):
    # Initialize AppleMusic object
    apple_music = AppleMusic(cache="./cache", sync=1, skipVideo=False)

    # Get album ID and name from the provided link
    album_id = album_link.split('/')[-1].split('?')[0]  # Extract album ID from the URL
    album_name = album_link.split('/')[-2]  # Extract album name from the URL

    # Construct valid album link
    valid_album_link = f"https://music.apple.com/us/album/{album_name}/{album_id}"

    # Print constructed URL
    print("Constructed URL:", valid_album_link)

    # Get information about the album from the constructed valid link
    album_info = apple_music.getInfo(valid_album_link)

    # Check if any tracks are explicit
    is_explicit = any(track_info.get('rating', '') == 'Explicit' for track_info in album_info["streams"])

    # Modify album name and add [E] if explicit
    if is_explicit:
        album_name += " [E]"

    # Ask user for the directory to save the file
    save_directory = input("Please provide the directory path to save the 'album_infosse.txt' file: ")
    file_path = os.path.join(save_directory, "album_infosse.txt")
    print("album_infosse.txt -- saved")

    with open(file_path, "w") as file:
        # Write modified album name to file
        file.write(f"Album: {album_name}\n\n")
        file.write(f"Album_ID: {album_id}\n\n")

        # Write track details to file
        for track_info in album_info["streams"]:
            track_name = track_info['song']
            explicitness = track_info.get('rating', 'Not Explicit')
            credits = track_info.get('credits', {})
            credits_str = ", ".join([f"{role}: {', '.join(artists)}" for role, artists in credits.items()])

            # Add [E] to track name if explicit
            if explicitness == 'Explicit':
                track_name += " [E]"

            file.write(f"Track: {track_name}\n")
            file.write(f"Explicitness: {explicitness}\n")
            file.write(f"Credits: {credits_str}\n\n")

    # Rename album and track files
    rename_album_and_tracks(save_directory, album_name, album_info["streams"])

def rename_album_and_tracks(directory, album_name, tracks):
    while True:
        # Get the actual case-sensitive album directory name (handling potential errors)
        try:
            existing_album_dir = next(d for d in os.listdir(directory) if d.lower() == album_name.lower())
        except StopIteration:
            print(f"Error: Album directory '{album_name}' not found in '{directory}'")
            choice = input("Do you want to choose a different directory? (y/n): ").lower()
            if choice == 'y':
                directory = input("Enter the new directory path: ")
                continue  # Try again with the new directory
            else:
                return  # Exit the function if the user doesn't want to choose a different directory

        album_directory = os.path.join(directory, existing_album_dir)
        break  # Exit the loop if the directory is found

    try:
        os.rename(os.path.join(directory, album_name.replace(" [E]", "")), album_directory)  # Rename without [E] if it exists
    except FileNotFoundError:
        print(f"Warning: Album directory '{album_name}' not found for renaming.")

    
# Function to read the Apple Music album link from a file
def read_album_link_from_file(file_path):
    with open(file_path, "r") as file:
        album_link = file.readline().strip()
    return album_link

# Example usage
if __name__ == "__main__":
    # Ask user for the directory where apple_music_link.txt is located
    directory = input("Please provide the directory path where 'apple_music_link.txt' is located: ")
    file_path = os.path.join(directory, "apple_music_link.txt")
    
    # Read the Apple Music album link from the file
    album_link = read_album_link_from_file(file_path)
    
    # Check explicitness of the album
    check_explicitness(album_link)

add functionality like to rename album name from REVAMPED to REVAMPED [E] by adding explicit tag to it and then add explicit tag to tracks which is having multiple names like lrc and m4a files - 08. La La Land (feat. Nita Strauss) [Rock Version].m4a to 08. La La Land (feat. Nita Strauss) [Rock Version][E].m4a

when receiving api response it is having revamped which is lowercase and tracks are like La La Land (feat. Nita Strauss) [Rock Version]

so, give me code to check album case not sensitive and also take care of track names

in album folder it contains tracks, lrc files, txt, png, jpg

add print statements

folder structure contains 
D:\ALAC features(enabled)\apple-music-alac-atmos-downloader\AM-DL downloads\Demi Lovato\REVAMPED

inside REVAMPED im having tracks

the already above parses link from apple_music_link.txt and checks explicitness from api response