/-
Exponential sequence, round 2: the tractable piece of the missing
mathlib item 6.

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 exponential sequence of abelian groups
  0 -> Z -> C -> C* -> 1 is exact. The inclusion sends n to 2 pi i n,
  it is injective, its image is exactly the kernel of the exponential
  map (proved from `Complex.exp_eq_one_iff`), and the exponential map
  is surjective onto the nonzero complex numbers (proved from
  `Complex.exp_log`). This is the pointwise, or stalk level, content
  of the exponential sequence of sheaves
  0 -> Z -> O_X -> O_X^* -> 0: at each point of a complex manifold,
  a holomorphic function has a local logarithm exactly when it is
  nowhere zero, and the ambiguity of the logarithm is 2 pi i Z.
* PROVED: the bridge lemma `lefschetz_one_one_of_exact`. It states
  the long exact sequence fragment at H^2(X, Z) as exactness of the
  pair (c1, projection to H^{0,2}) in mathlib's `Function.Exact`
  sense, and derives algebraicity of every Hodge class of type (1,1)
  from it by applying `lefschetz_one_one`. So the conditional (1,1)
  theorem now has its hypothesis stated as exactness, and the
  exponential exactness proved here is the local input that exactness
  rests on.
* PROVED: the real structure lemma `real_eq_of_middle_and_02`. A real
  vector in the Hodge decomposition is determined by its (1,1) and
  (0,2) components, because conjugation recovers the (2,0) component
  from the (0,2) component. This is the abstract form of the fact
  that the real subspace H^2(X, R) sits inside H^{1,1} plus H^{0,2}
  once the (2,0) piece is known, and it is the step that makes the
  reality hypothesis `hreality` do its work.
* NOT PROVED, still blocked: the exponential sequence as a sequence
  of SHEAVES on a complex manifold, its long exact sequence in sheaf
  cohomology, and the Dolbeault identification H^2(X, O_X) with
  H^{0,2}(X). Those need a sheaf of holomorphic functions O_X and
  its sheaf of units, which mathlib v4.13.0 does not have (it has
  complex manifolds and Ext based sheaf cohomology, but no O_X), plus
  the local existence of holomorphic logarithms and the Dolbeault
  theorem. The gap is now exactly: sheafify the exact sequence proved
  here, take the long exact sequence, and identify H^2(X, O_X) with
  H^{0,2}(X). Nothing in this file claims that step.

The exact mathlib pieces still missing for an unconditional
formalization are listed in `data/hodge_formalization.md`.
-/

import Hodge.Basic
import Mathlib.Algebra.Exact
import Mathlib.Analysis.SpecialFunctions.Complex.Log
import Mathlib.Data.Complex.Exponential
import Mathlib.Algebra.Group.Prod

namespace Hodge

/-! ### The exponential map on the complex numbers -/

/-- The exponential map, from the additive group of complex numbers to
the multiplicative group of nonzero complex numbers. This is the map
whose sheaf version is the right hand map of the exponential sequence
0 -> Z -> O_X -> O_X^* -> 0. -/
noncomputable def expUnits (z : ℂ) : ℂˣ :=
  Units.mk0 (Complex.exp z) (Complex.exp_ne_zero z)

theorem expUnits_zero : expUnits 0 = 1 := by
  apply Units.ext
  simp [expUnits]

theorem expUnits_add (z w : ℂ) : expUnits (z + w) = expUnits z * expUnits w := by
  apply Units.ext
  simp [expUnits, Complex.exp_add]

/-- The kernel of the exponential map is exactly 2 pi i Z. This is the
exactness of the sequence at C: the image of the inclusion of Z is
the kernel of exp. -/
theorem expUnits_eq_one_iff (z : ℂ) :
    expUnits z = 1 ↔ ∃ n : ℤ, z = n * (2 * Real.pi * Complex.I) := by
  constructor
  · intro h
    have h1 : Complex.exp z = 1 := congrArg (fun u : ℂˣ => (u : ℂ)) h
    exact Complex.exp_eq_one_iff.mp h1
  · rintro ⟨n, rfl⟩
    apply Units.ext
    show Complex.exp (↑n * (2 * ↑Real.pi * Complex.I)) = 1
    exact Complex.exp_eq_one_iff.mpr ⟨n, rfl⟩

/-- The exponential map is surjective onto the nonzero complex numbers:
every nonzero w has a logarithm. This is the exactness of the sequence
at C^*: the map C -> C^* is onto. -/
theorem expUnits_surjective : Function.Surjective expUnits := by
  intro w
  refine ⟨Complex.log (w : ℂ), ?_⟩
  apply Units.ext
  show Complex.exp (Complex.log (w : ℂ)) = (w : ℂ)
  exact Complex.exp_log (Units.ne_zero w)

/-! ### The inclusion of Z, and exactness of 0 -> Z -> C -> C^* -> 1 -/

/-- The inclusion of the integers into the complex numbers as multiples
of 2 pi i, the left hand map of the exponential sequence. -/
noncomputable def twoPiIIncl (n : ℤ) : ℂ :=
  n * (2 * Real.pi * Complex.I)

theorem twoPiI_ne_zero : (2 : ℂ) * Real.pi * Complex.I ≠ 0 := by
  apply mul_ne_zero
  apply mul_ne_zero
  · norm_num
  · exact_mod_cast Real.pi_ne_zero
  · exact Complex.I_ne_zero

/-- The inclusion 2 pi i Z -> C is injective, so the sequence is exact
at Z as well: the map 0 -> Z -> C has trivial kernel. -/
theorem twoPiIIncl_injective : Function.Injective twoPiIIncl := by
  intro n m h
  simp only [twoPiIIncl] at h
  have h2 : (n : ℂ) = (m : ℂ) := mul_right_cancel₀ twoPiI_ne_zero h
  exact_mod_cast h2

/-- Exactness of the exponential sequence at C: the kernel of exp is
the image of the inclusion of Z. -/
theorem expSeq_exact_at_C (z : ℂ) :
    expUnits z = 1 ↔ ∃ n : ℤ, twoPiIIncl n = z := by
  rw [expUnits_eq_one_iff]
  constructor
  · rintro ⟨n, rfl⟩
    exact ⟨n, rfl⟩
  · rintro ⟨n, hn⟩
    exact ⟨n, hn.symm⟩

/-- The exponential sequence of abelian groups 0 -> Z -> C -> C^* -> 1
is exact at every term: the inclusion is injective, its image is the
kernel of exp, and exp is surjective. -/
theorem expSeq_exact :
    Function.Injective twoPiIIncl ∧
    (∀ z : ℂ, expUnits z = 1 ↔ ∃ n : ℤ, twoPiIIncl n = z) ∧
    Function.Surjective expUnits :=
  ⟨twoPiIIncl_injective, expSeq_exact_at_C, expUnits_surjective⟩

/-! ### The long exact sequence fragment, as exactness -/

/-- Projection of the Hodge decomposition onto the (0,2) piece, as an
additive group homomorphism. -/
def proj02 {W20 W11 W02 : Type*} [AddCommGroup W20] [AddCommGroup W11]
    [AddCommGroup W02] : HodgeCohom2 W20 W11 W02 →+ W02 where
  toFun v := v.2.2
  map_zero' := rfl
  map_add' _ _ := rfl

/-- Lefschetz (1,1) from exactness of the long exact sequence fragment.
The hypothesis `hexact` says the pair (c1, projection to H^{0,2}) is
exact at H^2(X, Z) in mathlib's `Function.Exact` sense: the kernel of
the projection is the image of the first Chern class map. In reality
this exactness IS the long exact sequence of the exponential sequence
of sheaves, together with the identification of H^2(X, O_X) with
H^{0,2}(X). Here it is a hypothesis, stated as exactness rather than
as a bare surjectivity, and the theorem discharges the (1,1) case
from it. The local input to that exactness, the exponential sequence
of abelian groups, is proved above as `expSeq_exact`. -/
theorem lefschetz_one_one_of_exact
    {W20 W11 W02 : Type*} [AddCommGroup W20] [AddCommGroup W11] [AddCommGroup W02]
    {H2Z : Type*} [AddCommGroup H2Z]
    {Pic : Type*} [AddCommGroup Pic]
    (ι : H2Z →+ HodgeCohom2 W20 W11 W02)
    (c1 : Pic →+ H2Z)
    (hexact : Function.Exact c1 (proj02.comp ι))
    (α : H2Z) (hα : IsHodge11 ι α) :
    IsAlgebraic c1 α := by
  have h : (proj02.comp ι) α = 0 := hα.2
  obtain ⟨D, hD⟩ := (hexact α).mp h
  exact ⟨D, hD⟩

/-! ### The real structure of the Hodge decomposition -/

/-- A real vector in the Hodge decomposition is determined by its (1,1)
and (0,2) components: conjugation recovers the (2,0) component from
the (0,2) component. In particular the real subspace H^2(X, R) injects
into H^{1,1} plus H^{0,2}, which is the abstract form of the step that
lets the reality hypothesis `hreality` turn vanishing of the (0,2)
component into a Hodge class of type (1,1). -/
theorem real_eq_of_middle_and_02 {W20 W11 W02 : Type*} [Zero W20] [Zero W02]
    (conj20 : W20 → W02) (conj02 : W02 → W20) (conj11 : W11 → W11)
    {v w : HodgeCohom2 W20 W11 W02}
    (hv : hodgeConj conj20 conj02 conj11 v = v)
    (hw : hodgeConj conj20 conj02 conj11 w = w)
    (h11 : v.2.1 = w.2.1) (h02 : v.2.2 = w.2.2) : v = w := by
  obtain ⟨a, b, c⟩ := v
  obtain ⟨a', b', c'⟩ := w
  have e1 : conj02 c = a := congrArg Prod.fst hv
  have e2 : conj02 c' = a' := congrArg Prod.fst hw
  simp only [Prod.mk.injEq] at h11 h02 ⊢
  refine ⟨?_, h11, h02⟩
  rw [← e1, ← e2, h02]

end Hodge
