/-
First Chern class as a connecting homomorphism, round 4: the tractable
piece of the missing mathlib items 3, 4 and 6, plus the Dolbeault
assessment.

STATUS: This file compiles under Lean 4 with mathlib. Every theorem in
it is checked by the Lean kernel with no `sorry`.

What is proved here, and what is not:

* PROVED: the first Chern class map as a connecting homomorphism. In
  any abelian category with Ext, a short exact sequence
  `0 -> S.X1 -> S.X2 -> S.X3 -> 0` has a connecting map
  `c1 : Ext X S.X3 1 ->+ Ext X S.X1 2` given by postcomposition with
  the extension class (`Hodge.chernClass`). The long exact sequence
  fragment around it is exact at both joints
  (`chernClass_exact_left`, `chernClass_exact_right`), proved from
  mathlib's `Abelian.Ext.covariant_sequence_exact_1'` and
  `covariant_sequence_exact_3'`.
* PROVED: the abstract form of `hsurj`. The map `c1` surjects onto the
  kernel of the map induced by `S.f` in degree 2
  (`chernClass_surj_onto_kernel`), proved from
  `Abelian.Ext.covariant_sequence_exact_1`. Specialized to sheaves,
  this is `sheaf_chernClass_surj_onto_kernel`: for an abstract short
  exact sequence of abelian sheaves `0 -> Z -> O -> U -> 0`, the first
  Chern class `H^1(U) -> H^2(Z)` surjects onto the kernel of
  `H^2(Z) -> H^2(O)`.
* PROVED: the sheaf category carries Ext, so the sheaf statements are
  not vacuous (`sheaf_hasExt`, from the standard derived category and
  `hasExt_of_hasDerivedCategory`).
* PROVED: the bridge theorem `lefschetz_one_one_of_sheaf_exact`. Given
  the sheaf long exact sequence fragment plus a Dolbeault isomorphism
  `H^2(O) ≃+ H^{0,2}` and the Hodge decomposition map, every Hodge
  class of type (1,1) is algebraic. The only remaining hypotheses are
  exactly the two analytic inputs: Dolbeault and the Hodge
  decomposition.
* NOT PROVED, still blocked: the Dolbeault isomorphism itself, and the
  sheaf `O_X` of holomorphic functions it is about. Mathlib v4.13.0
  has no differential forms on manifolds (no exterior derivative, no
  de Rham cohomology, no Dolbeault cohomology, no Wirtinger or d-bar
  operators; a grep for "dolbeault", "de rham" and "wirtinger" over
  mathlib finds nothing, and no `AlternatingMap` appears under
  `Mathlib/Geometry/Manifold`), and no sheaf of holomorphic functions
  (it has the sheaf of smooth functions and pointwise holomorphic
  functions on complex manifolds, but no holomorphic sheaf, so the
  sequence `0 -> Z -> O_X -> O_X^* -> 0` cannot be stated as sheaves).
  The c1 construction above is therefore stated for an abstract short
  exact sequence of sheaves; instantiating `S` to the exponential
  sequence of sheaves is the remaining step, and it needs `O_X`.
-/

import Hodge.Exponential
import Mathlib.Algebra.Homology.DerivedCategory.Ext.ExactSequences
import Mathlib.CategoryTheory.Sites.SheafCohomology.Basic
import Mathlib.CategoryTheory.Sites.Equivalence
import Mathlib.Algebra.Category.Grp.FilteredColimits

namespace Hodge

open CategoryTheory

universe u v w

/-! ### The first Chern class in an abstract abelian category -/

section General

variable {C : Type u} [Category.{v} C] [Abelian C] [HasExt.{w} C]
variable {S : ShortComplex C} (hS : S.ShortExact) (X : C)

/-- The first Chern class map, abstractly: the connecting homomorphism
obtained by postcomposing with the extension class of a short exact
sequence `0 -> S.X1 -> S.X2 -> S.X3 -> 0`. For the exponential
sequence of sheaves `0 -> Z -> O_X -> O_X^* -> 0` this is
`c1 : H^1(X, O_X^*) -> H^2(X, Z)`. -/
noncomputable def chernClass : Abelian.Ext X S.X₃ 1 →+ Abelian.Ext X S.X₁ 2 :=
  hS.extClass.postcomp X (show 1 + 1 = 2 from rfl)

/-- Exactness of the long exact sequence fragment at `Ext X S.X3 1`:
the kernel of `c1` is the image of the map induced by `S.g`. -/
theorem chernClass_exact_left :
    Function.Exact ((Abelian.Ext.mk₀ S.g).postcomp X (add_zero 1)) (chernClass hS X) := by
  have h := Abelian.Ext.covariant_sequence_exact₃' X hS 1 2 rfl
  rw [ShortComplex.ab_exact_iff_function_exact] at h
  exact h

/-- Exactness of the long exact sequence fragment at `Ext X S.X1 2`:
the kernel of the map induced by `S.f` is the image of `c1`. -/
theorem chernClass_exact_right :
    Function.Exact (chernClass hS X)
      ((Abelian.Ext.mk₀ S.f).postcomp X (add_zero 2)) := by
  have h := Abelian.Ext.covariant_sequence_exact₁' X hS 1 2 rfl
  rw [ShortComplex.ab_exact_iff_function_exact] at h
  exact h

/-- The abstract form of `hsurj`: `c1` surjects onto the kernel of the
map induced by `S.f` in degree 2. Every class in `Ext X S.X1 2`
killed by that map is the Chern class of a class in
`Ext X S.X3 1`. -/
theorem chernClass_surj_onto_kernel (x : Abelian.Ext X S.X₁ 2)
    (hx : ((Abelian.Ext.mk₀ S.f).postcomp X (add_zero 2)) x = 0) :
    ∃ y : Abelian.Ext X S.X₃ 1, chernClass hS X y = x := by
  have hx' : x.comp (Abelian.Ext.mk₀ S.f) (add_zero 2) = 0 := hx
  obtain ⟨y, hy⟩ := Abelian.Ext.covariant_sequence_exact₁ (hS := hS) (x₁ := x)
    (hx₁ := hx') (hn₀ := rfl)
  exact ⟨y, hy⟩

end General

/-! ### Sheaf cohomology: the Chern class of an abstract exponential sequence -/

section Sheaf

/-! The sheaves take values in `AddCommGrp.{max v u}`: the plus
construction underlying sheafification needs the target category to
live in universe `max v u` for a site `C : Type u` with
`Category.{v} C`. The sheaf statements are conditional on
`HasSheafify`, exactly as mathlib's own `Sheaf.H` is; the hypothesis
is not vacuous, as `hasSheafify_small_site` below shows for small
sites. -/

variable {C : Type u} [Category.{v} C] {J : GrothendieckTopology C}
variable [HasSheafify J AddCommGrp.{max v u}]

/-- The category of abelian sheaves on a site carries Ext groups, so
the sheaf cohomology groups `Sheaf.H` are not vacuous: the standard
derived category gives `HasExt`. The universe levels are the ones
computed by `HasDerivedCategory.standard` for the sheaf category:
the sheaf category lives in `Type (max (u + 1) (v + 1))` with
morphisms in `Type (max u v)`. -/
instance sheaf_hasExt : HasExt.{max (u + 1) (v + 1), max u v, max (u + 1) (v + 1)}
    (Sheaf J AddCommGrp.{max v u}) := by
  letI := HasDerivedCategory.standard (Sheaf J AddCommGrp.{max v u})
  exact hasExt_of_hasDerivedCategory _

/-- Sheafification exists for small sites, so the `HasSheafify`
hypothesis of the sheaf theorems above is satisfiable: it holds
whenever the site is a small category. -/
theorem hasSheafify_small_site {C : Type u} [Category.{u} C]
    {J : GrothendieckTopology C} : HasSheafify J AddCommGrp.{u} := by
  infer_instance

/-- The constant sheaf with value `Z`, the coefficient object of sheaf
cohomology. -/
noncomputable abbrev sheafZ : Sheaf J AddCommGrp.{max v u} :=
  (constantSheaf J AddCommGrp.{max v u}).obj (AddCommGrp.of (ULift ℤ))

/-- The first Chern class map in sheaf cohomology: for an abstract short
exact sequence of abelian sheaves `0 -> S.X1 -> S.X2 -> S.X3 -> 0`,
the connecting homomorphism `H^1(S.X3) ->+ H^2(S.X1)`. For the
exponential sequence this is `c1 : H^1(X, O_X^*) -> H^2(X, Z)`. -/
noncomputable def sheafChernClass {S : ShortComplex (Sheaf J AddCommGrp.{max v u})}
    (hS : S.ShortExact) : Sheaf.H S.X₃ 1 →+ Sheaf.H S.X₁ 2 :=
  chernClass hS sheafZ

/-- Exactness at `H^1(S.X3)`: the kernel of `c1` is the image of the
map induced by `S.g`. -/
theorem sheaf_chernClass_exact_left {S : ShortComplex (Sheaf J AddCommGrp.{max v u})}
    (hS : S.ShortExact) :
    Function.Exact ((Abelian.Ext.mk₀ S.g).postcomp sheafZ (add_zero 1))
      (sheafChernClass hS) :=
  chernClass_exact_left hS sheafZ

/-- Exactness at `H^2(S.X1)`: the kernel of the map induced by `S.f`
is the image of `c1`. -/
theorem sheaf_chernClass_exact_right {S : ShortComplex (Sheaf J AddCommGrp.{max v u})}
    (hS : S.ShortExact) :
    Function.Exact (sheafChernClass hS)
      ((Abelian.Ext.mk₀ S.f).postcomp sheafZ (add_zero 2)) :=
  chernClass_exact_right hS sheafZ

/-- The abstract `hsurj` in sheaf cohomology: `c1` surjects onto the
kernel of `H^2(S.X1) -> H^2(S.X2)`. Every class in `H^2(S.X1)`
killed by the map induced by `S.f` is the Chern class of a class in
`H^1(S.X3)`. -/
theorem sheaf_chernClass_surj_onto_kernel
    {S : ShortComplex (Sheaf J AddCommGrp.{max v u})} (hS : S.ShortExact)
    (x : Sheaf.H S.X₁ 2)
    (hx : ((Abelian.Ext.mk₀ S.f).postcomp sheafZ (add_zero 2)) x = 0) :
    ∃ y : Sheaf.H S.X₃ 1, sheafChernClass hS y = x :=
  chernClass_surj_onto_kernel hS sheafZ x hx

/-! ### Bridge: Lefschetz (1,1) from the sheaf long exact sequence -/

/-- Lefschetz (1,1) from the sheaf long exact sequence, modulo the two
analytic inputs. Given an abstract short exact sequence of sheaves, a
Dolbeault isomorphism `e : H^2(S.X2) ≃+ H^{0,2}` and the Hodge
decomposition map `ι` whose (0,2) component is `e` composed with the
map induced by `S.f`, every Hodge class of type (1,1) is algebraic.
The remaining hypotheses are exactly the two blocked pieces: the
Dolbeault identification and the Hodge decomposition. -/
theorem lefschetz_one_one_of_sheaf_exact
    {W20 W11 W02 : Type*} [AddCommGroup W20] [AddCommGroup W11] [AddCommGroup W02]
    {S : ShortComplex (Sheaf J AddCommGrp.{max v u})} (hS : S.ShortExact)
    (ι : Sheaf.H S.X₁ 2 →+ HodgeCohom2 W20 W11 W02)
    (e : Sheaf.H S.X₂ 2 ≃+ W02)
    (hι : proj02.comp ι =
      e.toAddMonoidHom.comp ((Abelian.Ext.mk₀ S.f).postcomp sheafZ (add_zero 2)))
    (α : Sheaf.H S.X₁ 2) (hα : IsHodge11 ι α) :
    IsAlgebraic (sheafChernClass hS) α := by
  apply lefschetz_one_one_of_exact ι (sheafChernClass hS) _ α hα
  have hex : Function.Exact (sheafChernClass hS)
      ((Abelian.Ext.mk₀ S.f).postcomp sheafZ (add_zero 2)) :=
    sheaf_chernClass_exact_right hS
  rw [hι]
  intro y
  have key : (e.toAddMonoidHom.comp
      ((Abelian.Ext.mk₀ S.f).postcomp sheafZ (add_zero 2))) y = 0 ↔
      ((Abelian.Ext.mk₀ S.f).postcomp sheafZ (add_zero 2)) y = 0 := by
    show e (((Abelian.Ext.mk₀ S.f).postcomp sheafZ (add_zero 2)) y) = 0 ↔
      ((Abelian.Ext.mk₀ S.f).postcomp sheafZ (add_zero 2)) y = 0
    constructor
    · intro h
      exact e.injective (h.trans (map_zero e).symm)
    · intro h
      rw [h, map_zero]
  exact key.trans (hex y)

end Sheaf

end Hodge
