using SatelliteProvider.Common.Enums;

namespace SatelliteProvider.DataAccess.Models;

public class TileEntity
{
    public Guid Id { get; set; }
    public int TileZoom { get; set; }
    public int TileX { get; set; }
    public int TileY { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public double TileSizeMeters { get; set; }
    public int TileSizePixels { get; set; }
    public string ImageType { get; set; } = string.Empty;
    public string? MapsVersion { get; set; }
    public int? Version { get; set; }
    public string FilePath { get; set; } = string.Empty;
    // AZ-484: stored as the contract wire value (snake_case string). See
    // TileSourceConverter for enum<->wire conversion. Cannot use the
    // TileSource enum directly because Dapper bypasses TypeHandler<T> for
    // enum types during read deserialization (Dapper issue #259).
    public string Source { get; set; } = TileSourceConverter.GoogleMapsWireValue;
    public DateTime CapturedAt { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }

    // AZ-503: per-UAV-flight identifier. NULL for google_maps and pre-AZ-503
    // legacy UAV rows; populated for AZ-503+ UAV uploads. Part of the UPSERT
    // conflict key (via COALESCE to the zero-UUID) so two flights uploading
    // the same (z, x, y) cell coexist as distinct rows.
    public Guid? FlightId { get; set; }

    // AZ-503: UUIDv5(TILE_NAMESPACE, "{tile_zoom}/{tile_x}/{tile_y}"). Always
    // populated (column is NOT NULL after migration 014); deterministic across
    // C# and Python (see SatelliteProvider.Common.Utils.Uuidv5).
    public Guid LocationHash { get; set; }

    // AZ-503: SHA-256 of the tile body at insert time. NULL for pre-AZ-503
    // legacy rows (the migration cannot read tile files); NOT NULL by
    // application invariant for AZ-503+ inserts (TileService.BuildTileEntity
    // and UavTileUploadHandler.PersistAsync compute this before insert).
    public byte[]? ContentSha256 { get; set; }

    // AZ-503: pre-AZ-503 random Id value, preserved for one deprecation cycle
    // so external references to the legacy random id can be reconciled (per
    // AZ-503 Risk 1 mitigation). Dropped in a follow-up migration.
    public Guid? LegacyId { get; set; }
}
