import pathlib
import click


def get_filenames_from_template(photo, filename_template, original_name):
    """ get list of export filenames for a photo

    Args:
        photo: a PhotoInfo instance
        filename_template: a PhotoTemplate template string, may be None
        original_name: boolean; if True, use photo's original filename instead of current filename
    
    Returns:
        list of filenames
    
    Raises:
        click.BadOptionUsage if template is invalid
    """
    if filename_template:
        photo_ext = pathlib.Path(photo.original_filename).suffix
        filenames, unmatched = photo.render_template(filename_template, path_sep="_")
        if not filenames or unmatched:
            raise click.BadOptionUsage(
                "filename_template",
                f"Invalid template '{filename_template}': results={filenames} unmatched={unmatched}",
            )
        filenames = [f"{file_}{photo_ext}" for file_ in filenames]
    else:
        filenames = [photo.original_filename] if original_name else [photo.filename]
    return filenames