from pathlib import Path
import csv


class CSVWriter():
    def __init__(self) -> None:
        pass

    def _get_verified_output_path(self, output: str, force: bool) -> Path:
        path = Path(output)
        if path.suffix and path.suffix != '.csv':
            raise ValueError(f"Cannot write to suffix {path.suffix}")
        elif not path.suffix:
            path = path.with_suffix(".csv")

        file_path = path.with_suffix(".csv")
        if file_path.exists() and not force:
            raise FileExistsError(
                "Output .csv file already exists. You can overwrite using --force.")
        return file_path

    def write(self, output_path: str, csv_object: list[list[str]], delimiter: str, force: bool) -> None:
        path = self._get_verified_output_path(output_path, force)
        with open(path, 'w') as f:
            writer = csv.writer(f, delimiter=delimiter)
            writer.writerows(csv_object)
