{- | The free left fibration on a functor `q : X ->⁺ B`.
 -
 -   Left fibrations are exactly the maps right orthogonal to `dleft : 1 ->⁺ DI` ({leftFam-right-char}).
 -   Factoring {q} with respect to the factorization system generated by `dleft` ({InfCat.OFS.CatAccessibleOFS})
 -   gives `X ->⁺ CatFactor q ->⁺ B`, where the right part is a left fibration ({freeLeft-leftFib})
 -   and the left part is left orthogonal to every left fibration ({freeLeft-univ}).
 -}

\import Equiv
\import Equiv.Fiber
\import Function.Meta
\import Homotopy.Fibration
\import InfCat.Fibration.Categorical
\import InfCat.Fibration.Covariant.CovariantLift
\import InfCat.Fibration.Covariant.LeftFib
\import InfCat.OFS.CatAccessibleOFS
\import InfCat.OFS.CatOFS
\import Logic
\import Logic.Unique
\import Paths

-- | The family consisting of the single map `dleft : 1 ->⁺ DI`.
\func leftFam : CatFactor.Family \cowith
  | J => \Sigma
  | X _ => \Sigma
  | Y _ => DI
  | F _ _ => dleft

-- | The right maps of {leftFam} are the left fibrations.
\lemma leftFam-right-char {E B : \Cat} {p : E ->⁺ B} : leftFam.IsRight p <-> IsLeftFib (Fibers p)
  => (\lam r => leftFib-sections.2 \lam v => IsEquiv.trans (IsOrthogonal-sections.1 (r ()) v) (inP terminal-sections-equiv),
      \lam Ff _ => IsOrthogonal-sections.2 \lam v => IsEquiv.leftFactor (inP terminal-sections-equiv) (leftFib-sections.1 Ff v))
  \where {
    -- | Sections over {DI} are the starting point together with a lift.
    \func DI-sections-equiv {A : \Cat} (F : A ->⁺ \Cat) (v : DI ->⁺ A)
      : QEquiv {\Pi (i :⁺ DI) -> F (v i)} {\Sigma (b : F (v dleft)) (CovLift F (_, _, dpath v, b))} \cowith
      | f s => (s dleft, (s dright, dpath s))
      | ret t => \lam i => t.2.2 i
      | ret_f => idpe
      | f_sec => idpe

    -- | {F} is a left fibration if and only if sections over every arrow are determined by their starting point.
    \lemma leftFib-sections {A : \Cat} {F : A ->⁺ \Cat}
      : IsLeftFib F <-> (\Pi (v : DI ->⁺ A) -> IsEquiv (\lam (s : \Pi (i :⁺ DI) -> F (v i)) => s dleft))
      => (\lam Ff v => IsEquiv.trans (inP (DI-sections-equiv F v)) $ contrFibers=>IsEquiv \lam b =>
            Contr.Contr_QEquiv (symQEquiv (proj-fib _ b)) (Ff (_, _, dpath v, b)),
          \lam e d => Contr.Contr_QEquiv (proj-fib _ d.4) $
            IsEquiv=>contrFibers (IsEquiv.rightFactor (inP (DI-sections-equiv F (\lam i => d.3 i))) (e (\lam i => d.3 i))) d.4)

    -- | Functors out of the terminal category are objects.
    \func terminal-sections-equiv {T : \Cat} : QEquiv {\Sigma ->⁺ T} {T} \cowith
      | f s => s ()
      | ret t _ => t
      | ret_f => idpe
      | f_sec => idpe
  }

-- | The total category of the free left fibration on {q}.
\func FreeLeftTot {X B : \Cat} (q : X ->⁺ B) : \Cat
  => CatFactor {leftFam} q

-- | The projection of {FreeLeftTot} to the base.
\func freeLeftProj {X B : \Cat} {q : X ->⁺ B} (d :⁺ FreeLeftTot q) : B
  => catFactorRight {leftFam} d

-- | The inclusion of {X} into {FreeLeftTot}.
\func freeLeftIncl {X B : \Cat} {q : X ->⁺ B} (x :⁺ X) : FreeLeftTot q
  => catFactorLeft {leftFam} x

-- | The free left fibration on {q}.
\func FreeLeft {X B : \Cat} (q : X ->⁺ B) (b :⁺ B) : \Cat
  => Fibers (freeLeftProj {X} {B} {q}) b

\lemma freeLeft-leftFib {X B : \Cat} (q : X ->⁺ B) : IsLeftFib (FreeLeft q)
  => leftFam-right-char.1 (catFactorRight.isRight {leftFam})

-- | The unit of the free left fibration.
\func freeLeft-unit {X B : \Cat} (q : X ->⁺ B) (x :⁺ X) : FreeLeft q (q x)
  => (freeLeftIncl x, idp)

-- | {freeLeftIncl} is left orthogonal to every left fibration.
\lemma freeLeft-isLeft {X B E : \Cat} {q : X ->⁺ B} {h : E ->⁺ B} (hl : IsLeftFib (Fibers h))
  : IsOrthogonal (freeLeftIncl {X} {B} {q}) h
  => catFactorLeft.isLeft {leftFam} (leftFam-right-char.2 hl)

-- | The fibers of the total projection of a left fibration form a left fibration.
\lemma leftFib-total {B : \Cat} {Q : B ->⁺ \Cat} (Ql : IsLeftFib Q) : IsLeftFib (Fibers (totalProj Q))
  => leftFam-right-char.leftFib-sections.2 \lam v => IsEquiv.rightFactor (inP (fibers-total-equiv.sections v))
      (IsEquiv.trans (leftFam-right-char.leftFib-sections.1 Ql v) (inP (fibers-total-equiv (v dleft))))