import logging
import os
from datetime import date

logger = logging.getLogger(__name__)


def generate_tos_txt(
    organization_name: str,
    dataset_name: str,
    effective_date: str | None = None,
):
    """
    Generate contents of a terms of service (tos.txt) file for a dataset.

    :param organization_name: Name of the company or data steward.
    :param dataset_name: Name of the dataset to which these terms apply.
    :param effective_date: Optional effective date (default is today).
    :return: String content of tos.txt.
    """
    if effective_date is None:
        effective_date = date.today().isoformat()

    content = f"""Terms of Service (ToS) for {dataset_name}
Effective Date: {effective_date}

This dataset ("{dataset_name}") is made available by {organization_name}. By accessing or using this dataset,
you agree to the following terms:


1. Limited Quotation Rights
You may quote individual data points from this dataset in public communications (e.g. articles, papers, or posts),
provided no more than **two data points** are quoted at a time.

2. No Bulk Publishing
Publishing, redistributing, or sharing this dataset or any substantial portion of it in bulk, online or offline,
in raw or processed form, is **not permitted** under any circumstances.

3. Inheritance of Terms
These Terms of Service **inherit with the dataset**. Any derivatives, subsets, or distributions must include and uphold
these ToS. These terms apply regardless of how the dataset is accessed or transformed.

4. No AI Training
You may not include this dataset in the training corpus of any AI model for any part of its training process.

These Terms of Service are intended as a clear statement of intent and a condition of use. They are not a substitute for
legal advice. {organization_name} provides this dataset "as is" without warranty of any kind.
"""
    return content


def save_tos_txt(
    organization_name: str,
    dataset_name: str,
    effective_date: str | None = None,
    path: str = "tos.txt",
    verbose: bool = False,
):
    """
    Save the generated TOS content to a file. If path is a directory,
    create a tos.txt file inside it.
    """
    if os.path.isdir(path):
        path = os.path.join(path, "tos.txt")
    content = generate_tos_txt(organization_name, dataset_name, effective_date)
    with open(path, "w") as f:
        f.write(content)
    if verbose:
        logger.info(f"'tos.txt' has been written to {path}")
