package dev.photosync.core.timeline;

import dev.photosync.core.provider.RemoteAsset;
import dev.photosync.core.provider.TimelineBucket;

import java.time.LocalDate;
import java.util.List;

/**
 * One labelled run of tiles in the browser.
 *
 * <p>The backend groups by month; the player asked to browse by day. Those two
 * facts are reconciled here rather than in the renderer: a month that has not
 * been fetched yet contributes a single {@link PendingMonth} whose asset count
 * is known exactly, and the moment its page arrives it is replaced by one
 * {@link Day} per calendar day inside it.
 *
 * <p>Because the count is exact either way, the list has its true height before
 * anything is loaded -- so the scrollbar is honest from the first frame and the
 * browser only ever fetches the months the viewport actually reaches.
 */
public sealed interface TimelineSection {

    /** The day, or the first of the month for a section that is still pending. */
    LocalDate date();

    int assetCount();

    /** Assets known so far. Empty while pending, which is what makes a tile a placeholder. */
    List<RemoteAsset> assets();

    record PendingMonth(TimelineBucket bucket) implements TimelineSection {

        @Override
        public LocalDate date() {
            return bucket.month();
        }

        @Override
        public int assetCount() {
            return bucket.assetCount();
        }

        @Override
        public List<RemoteAsset> assets() {
            return List.of();
        }
    }

    record Day(LocalDate date, List<RemoteAsset> assets) implements TimelineSection {

        public Day {
            assets = List.copyOf(assets);
        }

        @Override
        public int assetCount() {
            return assets.size();
        }
    }
}
