from .base import Storage  # noqa
from reflector.settings import settings


def get_transcripts_storage() -> Storage:
    """
    Get storage for processed transcript files (master credentials).

    Also use this for ALL our file operations with bucket override:
        master = get_transcripts_storage()
        master.delete_file(key, bucket=recording.bucket_name)
    """
    assert settings.TRANSCRIPT_STORAGE_BACKEND
    return Storage.get_instance(
        name=settings.TRANSCRIPT_STORAGE_BACKEND,
        settings_prefix="TRANSCRIPT_STORAGE_",
    )


def get_source_storage(platform: str) -> Storage:
    """Get storage for reading/deleting source recording files from the platform's bucket.

    Returns an AwsStorage configured with the platform's worker credentials
    (access keys), or falls back to get_transcripts_storage() when platform-specific
    credentials aren't configured (e.g., single-bucket setups).

    Args:
        platform: Recording platform name ("daily", "whereby", or other).
    """
    if platform == "daily":
        if (
            settings.DAILYCO_STORAGE_AWS_ACCESS_KEY_ID
            and settings.DAILYCO_STORAGE_AWS_SECRET_ACCESS_KEY
            and settings.DAILYCO_STORAGE_AWS_BUCKET_NAME
        ):
            from reflector.storage.storage_aws import AwsStorage

            return AwsStorage(
                aws_bucket_name=settings.DAILYCO_STORAGE_AWS_BUCKET_NAME,
                aws_region=settings.DAILYCO_STORAGE_AWS_REGION or "us-east-1",
                aws_access_key_id=settings.DAILYCO_STORAGE_AWS_ACCESS_KEY_ID,
                aws_secret_access_key=settings.DAILYCO_STORAGE_AWS_SECRET_ACCESS_KEY,
            )

    elif platform == "whereby":
        if (
            settings.WHEREBY_STORAGE_AWS_ACCESS_KEY_ID
            and settings.WHEREBY_STORAGE_AWS_SECRET_ACCESS_KEY
            and settings.WHEREBY_STORAGE_AWS_BUCKET_NAME
        ):
            from reflector.storage.storage_aws import AwsStorage

            return AwsStorage(
                aws_bucket_name=settings.WHEREBY_STORAGE_AWS_BUCKET_NAME,
                aws_region=settings.WHEREBY_STORAGE_AWS_REGION or "us-east-1",
                aws_access_key_id=settings.WHEREBY_STORAGE_AWS_ACCESS_KEY_ID,
                aws_secret_access_key=settings.WHEREBY_STORAGE_AWS_SECRET_ACCESS_KEY,
            )

    elif platform == "livekit":
        if (
            settings.LIVEKIT_STORAGE_AWS_ACCESS_KEY_ID
            and settings.LIVEKIT_STORAGE_AWS_SECRET_ACCESS_KEY
            and settings.LIVEKIT_STORAGE_AWS_BUCKET_NAME
        ):
            from reflector.storage.storage_aws import AwsStorage

            return AwsStorage(
                aws_bucket_name=settings.LIVEKIT_STORAGE_AWS_BUCKET_NAME,
                aws_region=settings.LIVEKIT_STORAGE_AWS_REGION or "us-east-1",
                aws_access_key_id=settings.LIVEKIT_STORAGE_AWS_ACCESS_KEY_ID,
                aws_secret_access_key=settings.LIVEKIT_STORAGE_AWS_SECRET_ACCESS_KEY,
                aws_endpoint_url=settings.LIVEKIT_STORAGE_AWS_ENDPOINT_URL,
            )

    return get_transcripts_storage()


def get_whereby_storage() -> Storage:
    """
    Get storage config for Whereby (for passing to Whereby API).

    Usage:
        whereby_storage = get_whereby_storage()
        key_id, secret = whereby_storage.key_credentials
        whereby_api.create_meeting(
            bucket=whereby_storage.bucket_name,
            access_key_id=key_id,
            secret=secret,
        )

    Do NOT use for our file operations - use get_transcripts_storage() instead.
    """
    if not settings.WHEREBY_STORAGE_AWS_BUCKET_NAME:
        raise ValueError(
            "WHEREBY_STORAGE_AWS_BUCKET_NAME required for Whereby with AWS storage"
        )

    return Storage.get_instance(
        name="aws",
        settings_prefix="WHEREBY_STORAGE_",
    )


def get_dailyco_storage() -> Storage:
    """
    Get storage config for Daily.co (for passing to Daily API).

    Uses role_arn only — access keys are excluded because they're for
    worker reads (get_source_storage), not for the Daily API.

    Usage:
        daily_storage = get_dailyco_storage()
        daily_api.create_meeting(
            bucket=daily_storage.bucket_name,
            region=daily_storage.region,
            role_arn=daily_storage.role_credential,
        )

    Do NOT use for our file operations - use get_transcripts_storage() instead.
    """
    if not settings.DAILYCO_STORAGE_AWS_BUCKET_NAME:
        raise ValueError(
            "DAILYCO_STORAGE_AWS_BUCKET_NAME required for Daily.co with AWS storage"
        )

    from reflector.storage.storage_aws import AwsStorage

    return AwsStorage(
        aws_bucket_name=settings.DAILYCO_STORAGE_AWS_BUCKET_NAME,
        aws_region=settings.DAILYCO_STORAGE_AWS_REGION or "us-east-1",
        aws_role_arn=settings.DAILYCO_STORAGE_AWS_ROLE_ARN,
    )
