using System.Security.Cryptography; using System.Text; using System.Text.Json; namespace GuestOps.Web; public sealed class PmsSnapshot : TenantDocument { public string ReservationId { get; set; } = ""; public string Confirmation { get; set; } = ""; public string GuestName { get; set; } = ""; public string Arrival { get; set; } = ""; public string Departure { get; set; } = ""; public string Status { get; set; } = ""; public string RoomType { get; set; } = ""; public string Total { get; set; } = ""; public string CommentsJson { get; set; } = "[]"; public string Fingerprint { get; set; } = ""; public string ConnectionRevision { get; set; } = ""; public DateTime FetchedAt { get; set; } = DateTime.UtcNow; public object View() => new { Id, ReservationId, Confirmation, GuestName, Arrival, Departure, Status, RoomType, Total, FetchedAt }; } public sealed class PmsChange : TenantDocument { public string ReservationId { get; set; } = ""; public PmsSnapshot Before { get; set; } = new(); public PmsSnapshot? After { get; set; } public string Kind { get; set; } = ""; public string Arrival { get; set; } = ""; public string Departure { get; set; } = ""; public string Note { get; set; } = ""; public string State { get; set; } = "Review"; public string Detail { get; set; } = "Review the reservation and proposed change before applying."; public string ProposedBy { get; set; } = ""; public string ApprovedBy { get; set; } = ""; public DateTime UpdatedAt { get; set; } = DateTime.UtcNow; public DateTime ExpiresAt { get; set; } = DateTime.UtcNow.AddMinutes(10); public long Version { get; set; } public static bool Active(string state) => state is "Review" or "Applying" or "NeedsReview"; public object View() => new { Id, ReservationId, Kind, Arrival, Departure, Note, State, Detail, ProposedBy, ApprovedBy, UpdatedAt, ExpiresAt, Version, before = Before.View(), after = After?.View() }; } public sealed record PmsLookupInput(string Confirmation); public sealed record PmsProposeInput(string SnapshotId, string Kind, string Arrival, string Departure, string Note); public sealed record PmsApproveInput(long Version, bool AvailabilityAndPriceChecked); public sealed record PmsControlsInput(long Version, bool Enabled); public sealed class PmsConflict(string message) : Exception(message); public sealed class PmsInvalid(string message) : Exception(message); public sealed class OhipProfile { public string BaseUrl { get; set; } = ""; public string HotelCode { get; set; } = ""; public string ClientId { get; set; } = ""; public string ClientSecret { get; set; } = ""; public string AppKey { get; set; } = ""; public string EnterpriseId { get; set; } = ""; public string Scope { get; set; } = "urn:opc:hgbu:ws:__myscopes__"; public bool WritesEnabled { get; set; } public string NoteType { get; set; } = "RESERVATION"; public string NoteLocation { get; set; } = "GEN"; // Write enablement is a stop control, not a credential identity: verification // must remain possible after an administrator turns writes off. public string Revision => Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(new { BaseUrl, HotelCode, ClientId, ClientSecret, AppKey, EnterpriseId, Scope, NoteType, NoteLocation })))); public static OhipProfile? Read(IConfiguration config, string hotel) { if (!System.Text.RegularExpressions.Regex.IsMatch(hotel, "^[a-f0-9]{32}$")) return null; var section = config.GetSection("Pms:Hotels:" + hotel); if (!section.Exists()) return null; var profile = section.Get(); if (profile == null || !Uri.TryCreate(profile.BaseUrl, UriKind.Absolute, out var uri) || uri.Scheme != "https" || uri.Port != 443 || uri.AbsolutePath != "/" || uri.UserInfo.Length > 0 || uri.Query.Length > 0 || uri.Fragment.Length > 0 || uri.IsLoopback) throw new PmsInvalid("The administrator must configure a valid HTTPS OHIP origin."); foreach (var value in new[] { profile.HotelCode, profile.ClientId, profile.ClientSecret, profile.AppKey, profile.Scope, profile.NoteType, profile.NoteLocation }) if (string.IsNullOrWhiteSpace(value) || value.Length > 4000 || value.IndexOfAny(['\r','\n']) >= 0) throw new PmsInvalid("The administrator must complete the OHIP connection settings."); if (profile.HotelCode.Length > 20 || profile.NoteType.Length > 20 || profile.NoteLocation.Length > 20 || profile.ClientId.Contains(':')) throw new PmsInvalid("Invalid OHIP identifiers."); return profile; } }