--[[----------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2025 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
and its suppliers and are protected by all applicable intellectual
property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material
is strictly forbidden unless prior written permission is obtained
from Adobe.

------------------------------------------------------------------------------]]

-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

-- WF.oz_client.managed_catalog
local WFCatPhotoScoring = require 'WFCatPhotoScoring'

--============================================================================--

local WFCatPhotoScoringTest = {}

--------------------------------------------------------------------------------

-- @mark Tests

--------------------------------------------------------------------------------

function WFCatPhotoScoringTest.rxTestCalculateCombinedAestheticRating( rxTest )

	rxTest:expectEqual( 0, WFCatPhotoScoring.calculateCombinedAestheticRating( {} ) )
	
	local docValue1 = {
		payload = {
			aesthetics = {
				score = 0.8,  -- Only score has weight of 1.0
				balancing = 0.5,  -- All others have weight of 0
				blur = 0.3,
				version = "1.0"  -- Should be ignored
			}
		}
	}

	rxTest:expectEqual( 0.8, WFCatPhotoScoring.calculateCombinedAestheticRating( docValue1 ) )
	
	local docValue2 = {
		payload = {
			aesthetics = {
				score = -0.5  -- Should be clamped to 0
			}
		}
	}

	rxTest:expectEqual( 0, WFCatPhotoScoring.calculateCombinedAestheticRating( docValue2 ) )

end

--------------------------------------------------------------------------------

function WFCatPhotoScoringTest.rxTestCalculateBaseImportance( rxTest )

	local docValue1 = {
		payload = {
			aesthetics = { score = 0.8 },
			xmp = {
				dc = {
					title = "Test",
					description = "Test caption"
				}
			},
			reviews = {
				{ flag = "pick" }
			},
			ratings = {
				{ rating = 5 }
			},
			develop = {
				fromDefaults = false,
				xmpCameraRaw = {}
			}
		}
	}

	local expected1 = 50 + 80 + 5 + 5 + 0 + ( 0.3 * 0.8 )  -- flag + rating + title + caption + hasEdits + aesthetic
	local actual1 = WFCatPhotoScoring.calculateBaseImportance( docValue1 )
	
	-- Round both values to one decimal place
	expected1 = math.floor(expected1 * 10 + 0.5) / 10
	actual1 = math.floor(actual1 * 10 + 0.5) / 10
	
	rxTest:expectEqual( expected1, actual1 )
	
	local docValue2 = {
		payload = {
			reviews = {
				{ flag = "reject" }
			}
		}
	}

	rxTest:expectEqual( -20, WFCatPhotoScoring.calculateBaseImportance( docValue2 ) )

end

--------------------------------------------------------------------------------

function WFCatPhotoScoringTest.rxTestCalculateBlurImportance( rxTest )

	local docValue1 = {
		payload = {
			blur = {
				desired = 1.0,
				undesired = 0.0,
				clear = 0.5
			}
		}
	}

	local expected1 = ( 1.0 * 0.8 ) + ( 0.0 * -0.2 ) + ( 0.5 * 0 )
	rxTest:expectEqual( expected1, WFCatPhotoScoring.calculateBlurImportance( docValue1 ) )
	
	local docValue2 = {
		payload = {}
	}

	rxTest:expectEqual( 0, WFCatPhotoScoring.calculateBlurImportance( docValue2 ) )

end

--------------------------------------------------------------------------------

function WFCatPhotoScoringTest.rxTestCalculateEyeQualityImportance( rxTest )

	local docValue1 = {
		payload = {
			eye_quality = {
				eye_sharpness = 80,
				retina_visible_prob = 0.9,
				sclera_visible_prob = 0.8
			},
			xmp = {
				['mwg-rs'] = {
					Regions = {
						RegionList = {
							{ Type = "Face" },
							{ Type = "Face" }
						}
					}
				}
			}
		}
	}

	rxTest:expectEqual( 80, WFCatPhotoScoring.calculateEyeQualityImportance( docValue1 ) )
	
	local docValue2 = {
		payload = {
			eye_quality = {
				eye_sharpness = 70  -- Below min_sharpness threshold
			},
			xmp = {
				['mwg-rs'] = {
					Regions = {
						RegionList = {
							{ Type = "Face" },
							{ Type = "Face" }
						}
					}
				}
			}
		}
	}

	rxTest:expectEqual( 0, WFCatPhotoScoring.calculateEyeQualityImportance( docValue2 ) )
	
	local docValue3 = {
		payload = {
			eye_quality = {
				eye_sharpness = 80
			},
			xmp = {
				['mwg-rs'] = {
					Regions = {
						RegionList = {
							{ Type = "Face" },
							{ Type = "Face" },
							{ Type = "Face" },
							{ Type = "Face" }
						}
					}
				}
			}
		}
	}

	rxTest:expectEqual( 0, WFCatPhotoScoring.calculateEyeQualityImportance( docValue3 ) )

end

--------------------------------------------------------------------------------

function WFCatPhotoScoringTest.rxTestGetEyeBlurMultiplier( rxTest )

	local docValue1 = {
		payload = {
			xmp = {
				['mwg-rs'] = {
					Regions = {
						RegionList = {
							{ Type = "Face" },
							{ Type = "Face" }
						}
					}
				}
			}
		}
	}

	rxTest:expectEqual( 1.0, WFCatPhotoScoring.getEyeBlurMultiplier( docValue1 ) )
	
	local docValue2 = {
		payload = {
			xmp = {
				['mwg-rs'] = {
					Regions = {
						RegionList = {}
					}
				}
			}
		}
	}

	rxTest:expectEqual( 0, WFCatPhotoScoring.getEyeBlurMultiplier( docValue2 ) )
	
	local docValue3 = {
		payload = {
			xmp = {
				['mwg-rs'] = {
					Regions = {
						RegionList = {
							{ Type = "Face" },
							{ Type = "Face" },
							{ Type = "Face" },
							{ Type = "Face" }
						}
					}
				}
			}
		}
	}

	rxTest:expectEqual( 0, WFCatPhotoScoring.getEyeBlurMultiplier( docValue3 ) )

end

--------------------------------------------------------------------------------

function WFCatPhotoScoringTest.rxTestCalculateImportance( rxTest )

	local docValue1 = {
		payload = {
			aesthetics = { score = 0.8 },
			xmp = {
				dc = {
					title = "Test",
					description = "Test caption"
				},
				['mwg-rs'] = {
					Regions = {
						RegionList = {
							{ Type = "Face" },
							{ Type = "Face" }
						}
					}
				}
			},
			reviews = {
				{ flag = "pick" }
			},
			ratings = {
				{ rating = 5 }
			},
			bestFrame = {
				frame = 0.9,
				face = 0.95,
			},
			blur = {
				desired = 1.0
			},
			eye_quality = {
				eye_sharpness = 80
			}
		},
		develop = {
			fromDefaults = false,
			xmpCameraRaw = {}
		}
	}

	local expected1 = 50 + 80 + ( 0.3 * 0.8 ) +  -- Base importance
					( 0.9 * 10 ) +              -- Frame score
					( 0.95 * 2 ) +              -- Face score (outlier)
					0.8 +                       -- Blur importance
					80 +                        -- Eye quality
					5 +                         -- Flag
					5                           -- Rating  

	rxTest:expectEqual( expected1, WFCatPhotoScoring.calculateImportance( docValue1 ) )
	
	local docValue2 = {
		payload = {}
	}

	rxTest:expectEqual( 0, WFCatPhotoScoring.calculateImportance( docValue2 ) )

end

--------------------------------------------------------------------------------

return WFRxTestCase.wrapSuite( 'WFCatPhotoScoringTest', WFCatPhotoScoringTest )
