import logging
import os
from datetime import date

logger = logging.getLogger(__name__)


def generate_tos_txt(
    organization_name: str = "Example Corp",
    dataset_name: str = "Example Dataset",
    contact_email: str = "support@example.com",
    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 contact_email: Email for contact.
    :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 **one data point** may be quoted at a time.
- The data point must be clearly attributed to "{dataset_name}" and include a citation if applicable.
- Quoting more than one data point in any online or offline medium is **strictly prohibited** without prior written
  consent from {organization_name}.

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. Intended Use
This dataset is provided for research, academic, and non-commercial purposes only. Any commercial use requires a
separate licensing agreement.

5. Termination
Your access to the dataset may be suspended or revoked at our sole discretion for violation of these terms.

6. Updates to Terms
We reserve the right to update these terms. Continued use of the dataset constitutes acceptance of any such changes.

7. Contact
Questions or requests for data use beyond the scope of these terms should be directed to {contact_email}.
"""
    return content


def save_tos_txt(path="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()
    with open(path, "w") as f:
        f.write(content)
    if verbose:
        logger.info(f"'tos.txt' has been written to {path}")


if __name__ == "__main__":
    save_tos_txt()
