package dev.photosync.core.provider;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;

/**
 * One item in the remote timeline, reduced to what the grid actually draws.
 *
 * <p>{@code localCapturedAt} is the wall-clock time where the photo was taken,
 * not where the player is sitting now. Day grouping uses it, so a screenshot
 * taken at 00:30 in Tokyo stays on the Tokyo day even when browsed from Berlin
 * -- which is what every photo app does and what users expect.
 *
 * <p>{@code aspectRatio} arrives with the timeline page, before any image data,
 * so the grid can lay out and reserve space without a single thumbnail request.
 */
public record RemoteAsset(
        String id,
        AssetKind kind,
        Instant capturedAt,
        LocalDateTime localCapturedAt,
        double aspectRatio,
        Optional<String> thumbHash,
        Duration duration) {

    public RemoteAsset {
        thumbHash = thumbHash == null ? Optional.empty() : thumbHash.filter(hash -> !hash.isBlank());
        duration = duration == null ? Duration.ZERO : duration;
        // A zero or negative ratio would divide by zero in the layout pass.
        if (!(aspectRatio > 0) || Double.isInfinite(aspectRatio)) {
            aspectRatio = 1.0;
        }
    }

    public LocalDate localDay() {
        return localCapturedAt.toLocalDate();
    }

    public boolean isVideo() {
        return kind == AssetKind.VIDEO;
    }
}
