{- | Localizations of categories with weak equivalences [1, Section 3.3].
 -   The concrete construction and the definition of {IsLocalization} are in {InfCat.Localization.Concrete}.
 -
 -   [1] Denis-Charles Cisinski, Bastiaan Cnossen, Kim Nguyen, Tashi Walde, Synthetic Category Theory, 2026
 -}

\import Equiv
\import Equiv.Sigma
\import Function.Meta
\import InfCat.Arrows
\import InfCat.Basics
\import InfCat.Functor.Conservative
\import InfCat.Functor.Equiv
\import InfCat.Functor.FullyFaithful
\import InfCat.Groupoid
\import InfCat.Localization.CatWeakEq
\import InfCat.Paths
\import InfCat.SubCat.Full
\import Logic
\import Paths
\import Paths.Meta

-- | If the composite of {h} with a fully faithful functor {f} inverts weak equivalences, then {h} does as well.
\lemma invertsWeakEq-postcomp-ff {C : CatWeakEq} {B D : \Cat} {f : B ->⁺ D} (fc : IsFFMap f) {h : C ->⁺ B} (hw : InvertsWeakEq (\lam y => f (h y)))
  : InvertsWeakEq h \cowith
  | hfunc-we w => IsFFMap.isConservative fc (hw.hfunc-we w)

-- | If {D} is a groupoid, then every functor inverts weak equivalences [1, Lemma 3.3.3].
\lemma invertsWeakEqCat-groupoid {C : CatWeakEq} {D : \Cat} (Dg : IsGroupoid D)
  : IsHEquiv (\lam F => FullSubCat.inc F)
  => ff-surj-equiv.1 (FullSubCat.inc-ff, \lam F => FullSubCat.inc-sat {C ->⁺ D} {InvertingWeakEq C D} {F}
                                                    (\new FuncWeakEq { | hfunc-we {a} _ => Dg (dpath \lam i => F (a i)) }))

-- | Every functor sends isomorphisms to isomorphisms [1, Proposition 3.3.4].
\lemma invertsWeakEqCat-iso {C D : \Cat} : IsHEquiv {FuncWeakEqCat (MinCatWeakEq C) (MinCatWeakEq D)} {C ->⁺ D} FullSubCat.inc
  => ff-surj-equiv.1 (FullSubCat.inc-ff, \lam F => FullSubCat.inc-sat (FuncWeakEq.min F))

-- | Restriction along a functor inverting weak equivalences lands in `Fun_W(C,D)` [1, Construction 3.3.5].
\func loc-precomp {C : CatWeakEq} {L D : \Cat} {l : C ->⁺ L} (lw : InvertsWeakEq l) (G :⁺ L ->⁺ D) : FuncWeakEqCat C (MinCatWeakEq D)
  => res-sub l InvertsWeakEq (FuncWeakEq.compose-left lw) G
  \where {
    \sfunc inc_precomp (lw : InvertsWeakEq l) {G :⁺ L ->⁺ D} {x :⁺ C} : FullSubCat.inc (loc-precomp lw G) x = G (l x)
      => path \lam i => res-sub.inc_res i x

    {- | Restriction along {l} as a functor into the full subcategory of `C ->⁺ D` spanned by {P},
     -   where {lP} says that restrictions satisfy {P}.
     -   On objects this is `FullSubCat.make (\lam x => G (l x)) (lP G)`, but {FullSubCat.make} is invariant in its arguments,
     -   while this functor must be covariant in {G}, so it is obtained from {FullSubCat.isMax} instead.
     -}
    \func res-sub {C L D : \Cat} (l : C ->⁺ L) (P : (C ->⁺ D) -> \Prop) (lP : \Pi (G : L ->⁺ D) -> P (\lam x => G (l x))) (G :⁺ L ->⁺ D) : FullSubCat P
      => (FullSubCat.isMax {_} {P} {L ->⁺ D} {\lam G x => G (l x)} lP).1 G
      \where {
        \sfunc inc_res {G :⁺ L ->⁺ D} : FullSubCat.inc (res-sub l P lP G) = (\lam x => G (l x))
          => (FullSubCat.isMax {_} {P} {L ->⁺ D} {\lam G x => G (l x)} lP).2 G
      }
  }

-- | {l} is a localization of {C} with respect to functors into {D} [1, Definition 3.3.6].
\type IsLocalizationFor {C : CatWeakEq} {L : \Cat} (l : C ->⁺ L) (D : \Cat) : \Prop
  => \Sigma (lw : InvertsWeakEq l) (IsHEquiv (loc-precomp {C} {L} {D} lw))

-- | {l} is an epimorphism with respect to {D}: restriction `(L ->⁺ D) ->⁺ (C ->⁺ D)` along {l} is fully faithful.
\type IsEpiFor {C L : \Cat} (l : C ->⁺ L) (D : \Cat) : \Prop
  => IsFFMap {L ->⁺ D} {C ->⁺ D} (\lam G x => G (l x))

-- | Restriction along a localization is fully faithful.
\lemma localization-epi {C : CatWeakEq} {L D : \Cat} {l : C ->⁺ L} (loc : IsLocalizationFor l D) : IsEpiFor l D
  => transport IsFFMap (funExt+ \lam G => funExt+ \lam x => loc-precomp.inc_precomp loc.1)
       \lam G G' => IsEquiv.trans ((ff-surj-equiv.2 loc.2).1 G G') (FullSubCat.inc-ff _ _)

{- | A path of functors out of {l} determined by a pointwise path {p} of their restrictions; its restriction is {p}.
 -   It is obtained from the embedding of `L ->⁺ D` into `C ->⁺ D` given by restriction along {l}.
 -}
\sfunc epi-path {C L D : \Cat} {l : C ->⁺ L} (epi : IsEpiFor l D)
                {G1 G2 : L ->⁺ D} (p : \Pi (x :⁺ C) -> G1 (l x) = G2 (l x))
  : \Sigma (P : G1 = G2) (\Pi (x :⁺ C) -> pmap+ (\lam G => G (l x)) P = p x)
  => \let e => (IsFFMap.isObEmbedding {L ->⁺ D} {C ->⁺ D} {\lam G x => G (l x)} epi).isEmb G1 G2
     \in (e.sec (funExt+ p), \lam x => path \lam j => path \lam i => e.f_sec (funExt+ p) j i x)

-- | To check the universal property, it is enough to do this on objects for {D} and for `DI ->⁺ D` [1, Proposition 3.3.7].
\lemma localization-criterion {C : CatWeakEq} {L D : \Cat} {l : C ->⁺ L} (lw : InvertsWeakEq l)
                              (e : IsEquiv (FuncWeakEq.compose-left {C} {L} {D} lw)) (e' : IsEquiv (FuncWeakEq.compose-left {C} {L} {DI ->⁺ D} lw))
  : IsLocalizationFor l D
  => (lw, res-sub-equiv l (InvertingWeakEq C D) (FuncWeakEq.compose-left lw) (InvertingWeakEq C (DI ->⁺ D)) (FuncWeakEq.compose-left lw)
                          (\lam H => (\lam Hw => (\new FuncWeakEq { | hfunc-we s => (DI-iso-char.1 (Hw.hfunc-we s)).1 },
                                                  \new FuncWeakEq { | hfunc-we s => (DI-iso-char.1 (Hw.hfunc-we s)).2 }),
                                      \lam t => \new FuncWeakEq { | hfunc-we s => DI-iso-char.2 (t.1.hfunc-we s, t.2.hfunc-we s) }))
                          (IsEquiv.trans {_} {_} {_} {FuncWeakEq.compose-left lw} e {FuncWeakEq.sigma-equiv} (inP FuncWeakEq.sigma-equiv))
                          (IsEquiv.trans {_} {_} {_} {FuncWeakEq.compose-left lw} e' {FuncWeakEq.sigma-equiv} (inP FuncWeakEq.sigma-equiv)))
  \where {
    {- | Restriction along {l} is an equivalence onto the full subcategory spanned by {P} if it is an equivalence
     -   on objects onto {P} for {D} and onto {P'} for the arrow category `DI ->⁺ D`,
     -   where {P'} holds if and only if both endpoints satisfy {P}.
     -}
    \lemma res-sub-equiv {C L D : \Cat} (l : C ->⁺ L) (P : (C ->⁺ D) -> \Prop) (lP : \Pi (G : L ->⁺ D) -> P (\lam x => G (l x)))
      (P' : (C ->⁺ DI ->⁺ D) -> \Prop) (lP' : \Pi (G : L ->⁺ DI ->⁺ D) -> P' (\lam x => G (l x)))
      (Pe : \Pi (H : C ->⁺ DI ->⁺ D) -> P' H <-> (\Sigma (P (\lam x => H x dleft)) (P (\lam x => H x dright))))
      (e : IsEquiv {L ->⁺ D} {\Sigma (F : C ->⁺ D) (P F)} (\lam G => (\lam x => G (l x), lP G)))
      (e' : IsEquiv {L ->⁺ DI ->⁺ D} {\Sigma (H : C ->⁺ DI ->⁺ D) (P' H)} (\lam G => (\lam x => G (l x), lP' G)))
      : IsHEquiv (loc-precomp.res-sub l P lP)
      => ff-surj-equiv.1 (ffMap-leftFactor FullSubCat.inc-ff $ transportInv IsFFMap (funExt+ \lam G => loc-precomp.res-sub.inc_res) res-ff,
                          \lam d => \case IsEquiv.isSurj e (FullSubCat.inc d, FullSubCat.inc-con {_} {P} {d}) \with {
                            | inP (G,q) => inP (G, ((IsFFMap.isObEmbedding FullSubCat.inc-ff).isEmb _ _).sec $ loc-precomp.res-sub.inc_res *> pmap __.1 q)
                          })
      \where {
        \lemma res-ff : IsFFMap {L ->⁺ D} {C ->⁺ D} (\lam G x => G (l x))
          => \let | X => \Sigma (L ->⁺ D) (L ->⁺ D)
                  | Y => \Sigma (\Sigma (F : C ->⁺ D) (P F)) (\Sigma (F : C ->⁺ D) (P F))
                  | TY => \Sigma (j : Y) (j.1.1 ~> j.2.1)
                  | rmap (G : L ->⁺ DI ->⁺ D) : \Sigma (H : C ->⁺ DI ->⁺ D) (P' H) => (\lam x => G (l x), lP' G)
             \in \lam G1 G2 => fiberwiseEquiv (pair-equiv e) (\lam j p => dmap (\lam G x =>⁺ G (l x)) {j.1} {j.2} p)
              ((IsEquiv.parallelEquiv {_} {_} {_} {_} {rmap} (inP DI-fun-equiv) (inP pairs-equiv)
                \lam G => path \lam j => (((\lam x => G (l x) dleft, prop-pi j), (\lam x => G (l x) dright, prop-pi j)), dpath \lam i x => G (l x) i)).1 e')
              (G1, G2)

        -- | A functor to the arrow category whose endpoints satisfy {P} is a morphism of the full subcategory.
        \func pairs-equiv
          : QEquiv {\Sigma (H : C ->⁺ DI ->⁺ D) (P' H)}
                   {\Sigma (j : \Sigma (\Sigma (F : C ->⁺ D) (P F)) (\Sigma (F : C ->⁺ D) (P F))) (j.1.1 ~> j.2.1)} \cowith
          | f s => (((\lam x => s.1 x dleft, ((Pe s.1).1 s.2).1), (\lam x => s.1 x dright, ((Pe s.1).1 s.2).2)), dpath \lam i x => s.1 x i)
          | ret t => (\lam x i => t.2 i x, (Pe (\lam x i => t.2 i x)).2 (t.1.1.2, t.1.2.2))
          | ret_f s => ext idp
          | f_sec t => path \lam k => (((t.1.1.1, prop-pi {P t.1.1.1} {_} {t.1.1.2} k), (t.1.2.1, prop-pi {P t.1.2.1} {_} {t.1.2.2} k)), t.2)
      }
  }

-- | Functors that coincide after restriction along a localization are equal.
\sfunc loc-ext-eq {C : CatWeakEq} {L D : \Cat} {l : C ->⁺ L} (loc : IsLocalizationFor l D)
                  {G1 G2 : L ->⁺ D} (p : \Pi (x :⁺ C) -> G1 (l x) = G2 (l x)) {y :⁺ L} : G1 y = G2 y
  => path \lam i => (IsEquiv.isInj (IsHEquiv.toIsEquiv loc.2) $ ((IsFFMap.isObEmbedding FullSubCat.inc-ff).isEmb _ _).sec $
      ext \lam x => loc-precomp.inc_precomp loc.1 *> p x *> inv (loc-precomp.inc_precomp loc.1)) i y

-- | The extension along a localization of a functor inverting weak equivalences.
\func loc-lift {C : CatWeakEq} {L D : \Cat} {l : C ->⁺ L} (loc : IsLocalizationFor l D)
               {F : C ->⁺ D} (Fw : InvertsWeakEq F) (y :⁺ L) : D
  => IsHEquiv.ret loc.2 (FullSubCat.make F Fw) y
  \where {
    \func restrict {x :⁺ C} : loc-lift loc Fw (l x) = F x
      => inv (loc-precomp.inc_precomp loc.1) *> path (\lam i => FullSubCat.inc (IsHEquiv.f_ret loc.2 i) x)
  }

-- | A localization is unique up to a unique equivalence (Exercise 3.8.5).
\sfunc localization-unique {C : CatWeakEq} {L L' : \Cat} {l : C ->⁺ L} {l' : C ->⁺ L'}
                           (lL : IsLocalizationFor l L) (lL' : IsLocalizationFor l L')
                           (l'L : IsLocalizationFor l' L) (l'L' : IsLocalizationFor l' L')
  : \Sigma (u : L ->⁺ L') (\Pi (x :⁺ C) -> u (l x) = l' x) (IsHEquiv u)
  => \let | u => loc-lift lL' l'L'.1
          | v => loc-lift l'L lL.1
     \in (u, \lam x => loc-lift.restrict, inP \new HQEquiv {
            | finv => v
            | finv_f x => loc-ext-eq lL {\lam x => v (u x)} {\lam x => x} (\lam x => path (\lam i => v (loc-lift.restrict i)) *> loc-lift.restrict)
            | f_finv y => loc-ext-eq l'L' {\lam y => u (v y)} {\lam y => y} (\lam x => path (\lam i => u (loc-lift.restrict i)) *> loc-lift.restrict)
          })

-- | Functoriality of localizations [1, Construction 3.3.9].
\sfunc loc-map {C C' : CatWeakEq} {L L' : \Cat} {l : C ->⁺ L} {l' : C' ->⁺ L'}
               (loc : IsLocalizationFor l L') (l'S : InvertsWeakEq l') (f : FuncWeakEq C C')
  : \Sigma (m : L ->⁺ L') (\Pi (x :⁺ C) -> m (l x) = l' (f x))
  => (loc-lift loc (l'S FuncWeakEq.∘ f),
      \lam x => loc-lift.restrict {_} {_} {_} {_} {loc} {\lam x => l' (f x)})

-- | Restriction along a localization is an equivalence on functors into a groupoid (cf. Lemma 3.4.3).
\lemma loc-groupoid {C : CatWeakEq} {L D : \Cat} {l : C ->⁺ L} (loc : IsLocalizationFor l D) (Dg : IsGroupoid D)
  : IsHEquiv {L ->⁺ D} {C ->⁺ D} (\lam G x => G (l x))
  => transport IsHEquiv (funExt+ \lam G => funExt+ \lam x => loc-precomp.inc_precomp loc.1) $ IsHEquiv.trans loc.2 (invertsWeakEqCat-groupoid Dg)

-- | The identity functor is the localization at the isomorphisms.
\lemma id-isLocalizationFor-iso {C D : \Cat} : IsLocalizationFor {MinCatWeakEq C} (\lam x => x) D
  => \have lw => FuncWeakEq.min (\lam (x :⁺ C) => x)
     \in (lw, IsHEquiv.leftFactor invertsWeakEqCat-iso $ transportInv IsHEquiv (funExt+ \lam G => funExt+ \lam x => loc-precomp.inc_precomp lw) idHEquiv)

-- | For a localization, restriction is an equivalence on objects.
\lemma loc-restrict-equiv {C : CatWeakEq} {L D : \Cat} {l : C ->⁺ L} (loc : IsLocalizationFor l D)
  : IsEquiv {L ->⁺ D} {InvertingWeakEq C D} (FuncWeakEq.compose-left loc.1)
  => transport IsEquiv (ext \lam G => ext (ext \lam x => loc-precomp.inc_precomp loc.1))
                       (IsEquiv.trans (IsEquiv.trans (IsHEquiv.toIsEquiv loc.2) fullSub-ob-equiv) (inP (symQEquiv FuncWeakEq.sigma-equiv)))