\import Equiv
\import Function.Meta
\import Homotopy.HLevel
\import Homotopy.Truncation
\import InfCat.Basics
\import InfCat.Fibration.Univalence.DirectedUnivalence
\import InfCat.Fibration.Univalence.Straightening
\import InfCat.Paths
\import InfCat.SubCat.Full
\import InfCat.Trunc.IsCatTrunc
\import InfCat.Trunc.CatTrunc
\import InfCat.Trunc.TruncUniverse
\import InfCat.Yoneda.CovariantYoneda
\import InfCat.Yoneda.EncodeDecode
\import Logic
\import Meta
\import Paths
\import Paths.Meta

-- | The action of {catTrunc} on arrows, factored through the truncation.
\func catTrunc-hom-map.{u} {n : Nat} {C : \Cat u} {x y : C} (t : Trunc_-1+ n (x ~> y)) : catTrunc x ~> catTrunc y
  => Trunc_-1+.rec (CatTrunc.isTruncCat (catTrunc x) (catTrunc y)) (dmap catTrunc) t
  \where {
    \open FullSubCat

    -- | The functor `y ↦ Trunc_-1+ n (x ~> y)` extended to `CatTrunc n C`.
    \func codeL.{u} {C : \Cat u} (x : C) (y :⁺ CatTrunc n C) : TypeCat.{u} n
      => catTrunc-univ.lift TypeCat_IsTruncCat (\lam y' => (truncAdjunction n).ladj (ReprFunctor x y')) y

    \func code.{u} {C : \Cat u} (x : C) (y :⁺ CatTrunc n C) : \Type u
      => inc (codeL x y)

    \lemma code-level.{u} {C : \Cat u} {x : C} {y : CatTrunc n C} : code x y ofHLevel_-1+ n
      => inc-con {_} {_} {codeL x y}

    \sfunc code-equiv.{u} {C : \Cat u} {x y : C} : QEquiv {code x (catTrunc y)} {Trunc_-1+ n (x ~> y)}
      => transQEquiv truncAdjunction.ladj-equiv \new QEquiv {
           | f => Trunc_-1+.rec (Trunc_-1+.level n (x ~> y)).isTruncated \lam r => inT (Straight.fromStraight r)
           | ret => Trunc_-1+.rec (Trunc_-1+.level n (ReprFunctor x y)).isTruncated \lam r => inT (Straight.toStraight r)
           | ret_f => Trunc_-1+.elim-eq (Trunc_-1+.level n (ReprFunctor x y)).isTruncated \lam r => pmap inT (Straight.toStraight_fromStraight r)
           | f_sec => Trunc_-1+.elim-eq (Trunc_-1+.level n (x ~> y)).isTruncated \lam r => idp
         }
      \where
        -- | {code-equiv} maps the class of an arrow to its image under the unit of the truncation adjunction.
        \sfunc code-equiv_inT.{u} {C : \Cat u} {x y : C} {r : x ~> y}
          : code-equiv.ret (inT r) = ~>_-> ((truncAdjunction n).heta (ReprFunctor x y)) (Straight.toStraight r)
          => pmap (\lam (e : QEquiv {code x (catTrunc y)} {Trunc_-1+ n (x ~> y)}) => e.ret (inT r)) (\peval code-equiv {n} {C} {x} {y})

    -- | The representable functor of `catTrunc x` as a functor to {TypeCat}.
    \sfunc ReprFunctorL.{u} {C : \Cat u} (x : C)
      : \Sigma (h : CatTrunc n C ->⁺ TypeCat n) (\Pi (t :⁺ CatTrunc n C) -> inc (h t) = ReprFunctor (catTrunc x) t)
      => isMax {_} {_} {_} {ReprFunctor (catTrunc x)} \lam t => HLevel-retracts ReprFunctor.equiv (CatTrunc.isTruncCat (catTrunc x) t)

    \sfunc decode.{u} {C : \Cat u} {x : C}
      : \Sigma (d : \Pi (s :⁺ \Sigma⁺ (y : CatTrunc n C) (code x y)) -> catTrunc x ~> s.1)
               (\Pi {y : C} (p : x ~> y) -> d (catTrunc y, code-equiv.ret (inT p)) = dmap catTrunc p)
      => \let | (h,hp) => ReprFunctorL x
              | ff => catTrunc-ext-ff {n} {C} {TypeCat n} TypeCat_IsTruncCat (\lam t => codeL x t) h
              -- The arrow from `y ↦ (x ~> y)` to `y ↦ (catTrunc x ~> catTrunc y)` given by {catTrunc}.
              | a : (\lam y => ReprFunctor x y) ~> {C ->⁺ \Type u} (\lam y => inc (h (catTrunc y)))
                  => depUnivalence.ret \lam s => transport+ (\lam X => X) (inv (hp (catTrunc s.1))) (Straight.toStraight (dmap catTrunc (Straight.fromStraight s.2)))
              | b0 : (\lam t => codeL x (catTrunc t)) ~> {C ->⁺ TypeCat n} (\lam t => h (catTrunc t))
                  => dpath \lam i y => (truncAdjunction n).heta_heps-rl (dmap (__ y) a) i
              | b => IsEquiv.ret ff b0
              | d (s :⁺ \Sigma⁺ (y : CatTrunc n C) (code x y)) : catTrunc x ~> s.1
                  => Straight.fromStraight (transport+ (\lam X => X) (hp s.1) (depUnivalence (dmap (\lam H t =>⁺ inc (H t)) b) s))
         \in (d, \lam {y} p => pmap (\lam z => Straight.fromStraight (transport+ (\lam X => X) (hp (catTrunc y)) z)) (
                pmap (\lam (r : (\lam t => codeL x (catTrunc t)) ~> {C ->⁺ TypeCat n} (\lam t => h (catTrunc t))) => ~>_-> (dmap (\lam H => inc (H y)) r) (code-equiv.ret (inT p))) (IsEquiv.f_ret ff {b0})
                *> pmap (~>_-> (dmap inc ((truncAdjunction n).heta_heps-rl (dmap (__ y) a)))) code-equiv.code-equiv_inT
                *> inv (~>_->_comp _ _) *> pmap (~>_-> __ _) ((truncAdjunction n).heta_heps-rlr {_} {_} {dmap (__ y) a}))
              *> pmap Straight.fromStraight (transport_id_inv (\lam X => X) _ _))

    \func encodeDecode.{u} {C : \Cat u} {x : C} : EncodeDecode.{u} (catTrunc {n} x) \cowith
      | code => code x
      | ce => code-equiv.ret (inT idd)
      | decode => decode.1
      | decode_base => decode.2 idd

    \lemma isEquiv.{u} {C : \Cat u} {x y : C} : IsEquiv (catTrunc-hom-map {n} {C} {x} {y})
      => \have ed => encodeDecode.decode-equiv {catTrunc y} $ unfold \lam c => rewriteI (code-equiv.ret_f c) $
                      Trunc_-1+.elim-eq (code-level {n} {C} {x} {catTrunc y}) {\lam t => encodeDecode.encode (decode.1 (catTrunc y, code-equiv.ret t))} {code-equiv.ret}
                        (\lam p => pmap encodeDecode.encode (decode.2 p) *>
                          \let eta (y :⁺ C) => (truncAdjunction n).heta (ReprFunctor x y)
                          \in pmap (~>_-> (dmap (\lam y => code x (catTrunc y)) p)) code-equiv.code-equiv_inT
                              *> ~>_->-natural (\lam y a => ~>_-> (eta y) a) p
                              *> pmap (~>_-> (eta _)) (pmap (\lam S => S (_, Straight.toStraight p)) ((yonedaCov.Sec-equiv x (ReprFunctor x)).ret_f (\lam k => k.2)))
                              *> inv code-equiv.code-equiv_inT)
                        (code-equiv c)
         \in transport IsEquiv
              (ext \lam t => Trunc_-1+.elim-eq {n} {_} {catTrunc x ~> catTrunc y} (CatTrunc.isTruncCat _ _) (\lam p => decode.2 p) t)
              (inP (transQEquiv (symQEquiv code-equiv) ed))
  }