module Deontic.Civil.AcquisitivePrescription () where

import Deontic.Core.Types
import Deontic.Core.Verdict
import Deontic.Core.Adjudicate
import Deontic.Core.Layer (Base, Resolvable)
import Deontic.Civil.Types

-- ═══════════════════════════════════════════════
-- 취득시효 — Acquisitive Prescription (§245, §246)
--
-- §245①: 20년간 소유의 의사로 평온, 공연하게 부동산을
--         점유하는 자는 등기함으로써 그 소유권을 취득한다.
-- §245②: 부동산의 소유자로 등기한 자가 10년간 소유의 의사로
--         평온, 공연하게 선의이며 과실 없이 그 부동산을 점유한
--         때에는 소유권을 취득한다.
--
-- §157에 따라 역에 의한 계산 (addGregorianYearsClip).
-- ShortPrescription layer overrides for §245② (10-year).
-- ═══════════════════════════════════════════════

type instance Resolvable AcqPrescriptionAct = '[ShortPrescription, Base]

-- 제245조 제1항 (점유로 인한 부동산소유권의 취득기간)
instance Adjudicate AcqPrescriptionAct '[Base] where
  adjudicate :: AcqPrescriptionAct -> Facts AcqPrescriptionAct -> Judgment '[Base]
adjudicate AcqPrescriptionAct
_ Facts AcqPrescriptionAct
facts
    | AcqPrescFacts -> Bool
apfSelfPossession Facts AcqPrescriptionAct
AcqPrescFacts
facts
    , AcqPrescFacts -> Bool
apfPeaceful Facts AcqPrescriptionAct
AcqPrescFacts
facts
    , AcqPrescFacts -> Bool
apfPublic Facts AcqPrescriptionAct
AcqPrescFacts
facts
    , Integer -> Day -> Day
addGregorianYearsClip Integer
20 (AcqPrescFacts -> Day
apfStartDate Facts AcqPrescriptionAct
AcqPrescFacts
facts) Day -> Day -> Bool
forall a. Ord a => a -> a -> Bool
<= AcqPrescFacts -> Day
apfCurrentDate Facts AcqPrescriptionAct
AcqPrescFacts
facts =
        Verdict -> ArticleRef -> Text -> Judgment '[Base]
forall l. Verdict -> ArticleRef -> Text -> Judgment '[l]
JBase Verdict
Valid
          (Text -> Int -> Maybe Int -> ArticleRef
ArticleRef Text
"민법" Int
245 (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1))
          Text
"20년간 소유의 의사로 평온, 공연하게 부동산을 점유하여 취득시효가 완성되었다."
    | 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
245 (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1))
          Text
"취득시효의 요건이 충족되지 아니하였다."

-- 제245조 제2항 (단기 취득시효)
instance Adjudicate AcqPrescriptionAct rest
      => Adjudicate AcqPrescriptionAct (ShortPrescription ': rest) where
  adjudicate :: AcqPrescriptionAct
-> Facts AcqPrescriptionAct -> Judgment (ShortPrescription : rest)
adjudicate AcqPrescriptionAct
act Facts AcqPrescriptionAct
facts
    | AcqPrescFacts -> Bool
apfSelfPossession Facts AcqPrescriptionAct
AcqPrescFacts
facts
    , AcqPrescFacts -> Bool
apfPeaceful Facts AcqPrescriptionAct
AcqPrescFacts
facts
    , AcqPrescFacts -> Bool
apfPublic Facts AcqPrescriptionAct
AcqPrescFacts
facts
    , AcqPrescFacts -> Bool
apfGoodFaith Facts AcqPrescriptionAct
AcqPrescFacts
facts
    , AcqPrescFacts -> Bool
apfNoNegligence Facts AcqPrescriptionAct
AcqPrescFacts
facts
    , Integer -> Day -> Day
addGregorianYearsClip Integer
10 (AcqPrescFacts -> Day
apfStartDate Facts AcqPrescriptionAct
AcqPrescFacts
facts) Day -> Day -> Bool
forall a. Ord a => a -> a -> Bool
<= AcqPrescFacts -> Day
apfCurrentDate Facts AcqPrescriptionAct
AcqPrescFacts
facts =
        Judgment rest
-> Verdict
-> ArticleRef
-> Text
-> Judgment (ShortPrescription : rest)
forall (prev :: [*]) l.
Judgment prev
-> Verdict -> ArticleRef -> Text -> Judgment (l : prev)
JOverride (forall act (layers :: [*]).
Adjudicate act layers =>
act -> Facts act -> Judgment layers
adjudicate @_ @rest AcqPrescriptionAct
act Facts AcqPrescriptionAct
facts)
                  Verdict
Valid
                  (Text -> Int -> Maybe Int -> ArticleRef
ArticleRef Text
"민법" Int
245 (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
2))
                  Text
"10년간 소유의 의사로 평온, 공연하게 선의이며 과실 없이 부동산을 점유하여 단기 취득시효가 완성되었다."
    | Bool
otherwise =
        Judgment rest -> Judgment (ShortPrescription : rest)
forall (prev :: [*]) l. Judgment prev -> Judgment (l : prev)
JDelegate (forall act (layers :: [*]).
Adjudicate act layers =>
act -> Facts act -> Judgment layers
adjudicate @_ @rest AcqPrescriptionAct
act Facts AcqPrescriptionAct
facts)