import re
import sys

def split_org_project(file_path, project_title, chunk_size=50):
    with open(file_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()

    new_lines = []
    in_target_project = False
    project_indent = 0
    subtasks = []
    header_info = []
    
    # regex to find the project
    project_re = re.compile(r'^(\*+) (?:TODO |NEXT |DONE |CNCL )?' + re.escape(project_title))
    
    for i, line in enumerate(lines):
        match = project_re.match(line)
        if match:
            in_target_project = True
            project_indent = len(match.group(1))
            header_info.append(line)
            # Find the properties block of the project to preserve it
            continue
        
        if in_target_project:
            # If we hit another headline of the same or higher level, project ended
            headline_match = re.match(r'^(\*+) ', line)
            if headline_match and len(headline_match.group(1)) <= project_indent:
                # Process the collected subtasks before continuing
                process_chunks(new_lines, project_title, subtasks, chunk_size)
                in_target_project = False
                new_lines.append(line)
                continue
            
            # Collect everything in the project
            subtasks.append(line)
        else:
            new_lines.append(line)
            
    if in_target_project:
        process_chunks(new_lines, project_title, subtasks, chunk_size)

    with open(file_path, 'w', encoding='utf-8') as f:
        f.writelines(new_lines)

def process_chunks(target_list, title, subtasks, chunk_size):
    # Split subtasks into chunks based on Level 3 headlines (***)
    current_chunk = []
    chunk_count = 1
    task_count = 0
    
    for line in subtasks:
        if line.startswith('*** '):
            if task_count >= chunk_size:
                write_chunk(target_list, title, current_chunk, chunk_count)
                current_chunk = []
                chunk_count += 1
                task_count = 0
            task_count += 1
        current_chunk.append(line)
        
    if current_chunk:
        write_chunk(target_list, title, current_chunk, chunk_count)

def write_chunk(target_list, title, lines, part):
    target_list.append(f"** TODO {title} [Part {part}]\n")
    target_list.append(":PROPERTIES:\n")
    target_list.append(f":ID:       {title.replace(' ', '-').lower()}-part-{part}\n")
    target_list.append(":ORG_GTD:  Projects\n")
    target_list.append(":END:\n\n")
    target_list.extend(lines)

if __name__ == "__main__":
    split_org_project(sys.argv[1], sys.argv[2])
