import argparse
import os
import sys

# Assuming these modules will be created and populated later
from . import converter
from . import utils
from . import config

def main():
    parser = argparse.ArgumentParser(
        description="Convert video files to DaVinci Resolve compatible formats."
    )
    parser.add_argument(
        "input_file",
        help="Path to the input video file.",
        type=str
    )
    parser.add_argument(
        "-o", "--output_dir",
        help="Optional: Directory to save the converted file. Defaults to the input file's directory.",
        type=str,
        default=None
    )

    args = parser.parse_args()

    input_file_path = os.path.abspath(args.input_file)
    
    if not os.path.exists(input_file_path):
        print(f"Error: Input file not found at '{input_file_path}'", file=sys.stderr)
        sys.exit(1)
    
    if not os.path.isfile(input_file_path):
        print(f"Error: '{input_file_path}' is not a file.", file=sys.stderr)
        sys.exit(1)

    output_dir = args.output_dir
    if output_dir:
        output_dir = os.path.abspath(output_dir)
        if not os.path.isdir(output_dir):
            print(f"Error: Output directory '{output_dir}' does not exist or is not a directory.", file=sys.stderr)
            sys.exit(1)
    else:
        output_dir = os.path.dirname(input_file_path)

    # Perform conversion
    print(f"Starting conversion for {input_file_path}...")
    success = converter.convert_video(input_file_path, output_dir)

    if success:
        print("Conversion completed successfully.")
    else:
        print("Conversion failed.", file=sys.stderr)
        sys.exit(1)

if __name__ == "__main__":
    main()
