module Deontic.Civil.CoOwnership () where

import qualified Data.Set as Set
import Deontic.Core.Types
import Deontic.Core.Verdict
import Deontic.Core.Adjudicate
import Deontic.Core.Layer (Base, Resolvable)
import Deontic.Civil.Types

-- ═══════════════════════════════════════════════
-- 공유물의 처분 — Co-Ownership Disposition (§264)
--
-- This demonstrates universal quantification:
-- "공유자 전원의 동의" = ∀ owner ∈ owners, owner ∈ consented
--
-- No new framework feature needed — the open Facts type family
-- carries structured data, and the instance body uses standard
-- Haskell (all, Set.member) for the universal check.
-- ═══════════════════════════════════════════════

type instance Resolvable CoOwnershipAct = '[Base]

-- 제264조 (공유물의 처분, 변경)
-- "공유물의 처분 또는 변경은 공유자 전원의 동의가 있어야 한다."
instance Adjudicate CoOwnershipAct '[Base] where
  adjudicate :: CoOwnershipAct -> Facts CoOwnershipAct -> Judgment '[Base]
adjudicate CoOwnershipAct
_ Facts CoOwnershipAct
facts
    | (PersonId -> Bool) -> [PersonId] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (PersonId -> Set PersonId -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` CoOwnershipFacts -> Set PersonId
cofConsented Facts CoOwnershipAct
CoOwnershipFacts
facts) (CoOwnershipFacts -> [PersonId]
cofOwners Facts CoOwnershipAct
CoOwnershipFacts
facts) =
        Verdict -> ArticleRef -> Text -> Judgment '[Base]
forall l. Verdict -> ArticleRef -> Text -> Judgment '[l]
JBase Verdict
Valid
          (Text -> Int -> Maybe Int -> ArticleRef
ArticleRef Text
"민법" Int
264 Maybe Int
forall a. Maybe a
Nothing)
          Text
"공유물의 처분 또는 변경에 관하여 공유자 전원의 동의가 있다."
    | Bool
otherwise =
        Verdict -> ArticleRef -> Text -> Judgment '[Base]
forall l. Verdict -> ArticleRef -> Text -> Judgment '[l]
JBase Verdict
Void
          (Text -> Int -> Maybe Int -> ArticleRef
ArticleRef Text
"민법" Int
264 Maybe Int
forall a. Maybe a
Nothing)
          Text
"공유자 전원의 동의가 없으므로 공유물의 처분 또는 변경은 무효이다."