using System.Globalization; using System.Text.Json.Serialization; [JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Disallow)] internal sealed record FindPlantsRequest( int? Zone, string? Light, string? Soil, decimal? Size, bool Native = false, bool Fall = false) { public string? ValidationMessage() { if (Zone is null || string.IsNullOrEmpty(Light) || string.IsNullOrEmpty(Soil)) { return "Provide a hardiness zone, light setting, and soil moisture."; } if (Zone is < 1 or > 13 || Light is not ("sun" or "shade") || Soil is not ("dry" or "wet")) { return "Use a whole-number zone from 1 to 13, sun or shade, and dry or wet soil."; } return Size is < 1 or > 200 ? "Set the optional mature height from 1 to 200 feet." : null; } } internal sealed class FixtureCatalog(IConfiguration configuration) { private static readonly CatalogRow[] Rows = [ new("G-103", "Fixture shrub A", 6, 8, "shade", "wet", 6, Native: true, Fall: true), new("G-104", "Fixture shrub B", 7, 9, "shade", "wet", 4, Native: true, Fall: false), new("G-105", "Fixture groundcover", 5, 8, "shade", "dry", null, Native: null, Fall: null), new("G-106", "Fixture perennial", 6, 8, "sun", "wet", 3, Native: false, Fall: true) ]; public PlantResponse Find(FindPlantsRequest input) { if (configuration.GetValue("PlantFinder:SimulateCatalogUnavailable")) { throw new InvalidOperationException("Fixture catalog unavailable."); } var matches = Rows .Where(row => row.HeightFeet is > 0 && input.Zone >= row.ZoneMin && input.Zone <= row.ZoneMax && input.Light == row.Light && input.Soil == row.Soil) .OrderByDescending(row => Score(row, input)) .ThenBy(row => row.Id, StringComparer.Ordinal) .Take(3) .Select(row => Project(row, input)) .ToArray(); return matches.Length == 0 ? new("no-match", "No complete fixture record matches the required conditions.", matches) : new("match", $"{matches.Length} fixture matches. Review their reasons and source records.", matches); } private static int Score(CatalogRow row, FindPlantsRequest input) { var native = input.Native && row.Native == true ? 2 : 0; var fall = input.Fall && row.Fall == true ? 2 : 0; var size = input.Size is { } preferredHeight && row.HeightFeet <= preferredHeight ? 1 : 0; return native + fall + size; } private static PlantMatch Project(CatalogRow row, FindPlantsRequest input) { var reason = FormattableString.Invariant( $"Fits the requested zone, light and soil. Mature height {row.HeightFeet} ft."); if (input.Size is { } preferredHeight && row.HeightFeet > preferredHeight) { reason += $" This exceeds the preferred {preferredHeight.ToString("G29", CultureInfo.InvariantCulture)} ft."; } if (input.Native && row.Native == true) { reason += " Native tag."; } if (input.Fall && row.Fall == true) { reason += " Fall interest tag."; } return new PlantMatch(row.Id, row.Name, "#" + row.Id, reason); } private sealed record CatalogRow( string Id, string Name, int ZoneMin, int ZoneMax, string Light, string Soil, int? HeightFeet, bool? Native, bool? Fall); } internal sealed record PlantResponse(string Kind, string Message, PlantMatch[] Matches); internal sealed record PlantMatch(string Id, string Name, string Source, string Reason);