"""Landsat 8/9 Collection 2 Level-1 data from olmoearth_datasets."""

from datetime import timedelta

from olmoearth_shared.models.datasets.bands.validation import get_valid_bands
from olmoearth_shared.models.datasets.collection import Collection
from rslearn.data_sources.data_source import DataSourceContext

from .olmoearth_datasets import OlmoEarthDatasets, select_band_sets
from .retry_config import RetryConfig
from .search_items_query_filters import SearchItemsQueryFilters


class Landsat89C2L1(OlmoEarthDatasets):
    """Data source for Landsat 8/9 Collection 2 Level-1 data from olmoearth_datasets."""

    COLLECTION = Collection.LANDSAT_8_9_C2_L1

    def __init__(
        self,
        band_sets: list[list[str]] | None = None,
        query: SearchItemsQueryFilters | None = None,
        retry_config: RetryConfig | None = None,
        timeout: timedelta = timedelta(seconds=30),
        skip_items_missing_assets: bool = True,
        context: DataSourceContext = DataSourceContext(),
    ):
        """Initialize a new Landsat89C2L1 instance.

        Args:
            band_sets: list of band sets to ingest. If None, band sets are derived from
                the layer config (if available), otherwise all default band sets.
            query: query filters and sorting options.
            retry_config: configuration for HTTP request retries.
            timeout: timeout for API and download requests.
            skip_items_missing_assets: skip items that are missing any of the assets
                in band_sets during get_items.
            context: the data source context.
        """
        # Each band is contained in a single-band asset.
        available_band_sets = [[band] for band in get_valid_bands(self.COLLECTION)]
        selected_band_sets = select_band_sets(available_band_sets, band_sets, context)

        super().__init__(
            collection=self.COLLECTION,
            band_sets=selected_band_sets,
            query=query,
            retry_config=retry_config,
            timeout=timeout,
            skip_items_missing_assets=skip_items_missing_assets,
            context=context,
        )
