import Mathlib
import Development.AbelianVarietyTheory.Definitions.Divisors.EqZeroOfForallPowDvd

set_option autoImplicit false

noncomputable section

open MvPowerSeries GlobalTheory GlobalTheory.Katz GlobalTheory.QLog

namespace GlobalTheory.QLog.OpLin

variable {R : Type} [CommRing R]

theorem degree_eq_one {σ : Type} [Fintype σ] {D : σ →₀ ℕ} (h : D.degree = 1) :
    ∃ s, D = Finsupp.single s 1 := by
  classical
  have hsum := h
  rw [Finsupp.degree_eq_sum] at hsum
  obtain ⟨s, hs⟩ : ∃ s, D s ≠ 0 := by
    by_contra hc
    push_neg at hc
    simp [hc] at hsum
  refine ⟨s, ?_⟩
  have hle : ∀ t, t ≠ s → D s + D t ≤ 1 := fun t ht => by
    rw [← hsum, ← Finset.sum_pair (Ne.symm ht)]
    exact Finset.sum_le_sum_of_subset (Finset.subset_univ _)
  ext t
  by_cases ht : t = s
  · subst ht
    have : D t ≤ 1 := h ▸ Finsupp.le_degree t D
    simp only [Finsupp.single_eq_same]
    omega
  · have := hle t ht
    simp only [Finsupp.single_apply, if_neg (Ne.symm ht)]
    omega

theorem le_order_prod_pow {σ τ : Type} {a : σ → MvPowerSeries τ R}
    (ha : ∀ s, constantCoeff (a s) = 0) (D : σ →₀ ℕ) :
    (D.degree : ℕ∞) ≤ (D.prod fun s e => a s ^ e).order := by
  refine le_trans ?_ (le_order_prod _ _)
  rw [Finsupp.degree_apply, Nat.cast_sum]
  exact Finset.sum_le_sum fun s _ => le_order_pow_of_constantCoeff_eq_zero _ (ha s)

theorem coeff_subst_of_degree_le_one {σ τ : Type} [Fintype σ] {a : σ → MvPowerSeries τ R}
    (ha : ∀ s, constantCoeff (a s) = 0) {f : MvPowerSeries σ R} (hf : constantCoeff f = 0)
    {β : τ →₀ ℕ} (hβ : β.degree ≤ 1) :
    coeff β (subst a f) = ∑ s, coeff (Finsupp.single s 1) f * coeff β (a s) := by
  classical
  have hs : HasSubst a := hasSubst_of_constantCoeff_zero ha
  rw [coeff_subst hs]
  have hsupp : (Function.support fun D : σ →₀ ℕ =>
      coeff D f • coeff β (D.prod fun s e => a s ^ e)) ⊆
      ((Finset.univ.image fun s : σ => Finsupp.single s 1 : Finset (σ →₀ ℕ)) : Set (σ →₀ ℕ)) := by
    intro D hD
    rw [Function.mem_support] at hD
    have h0 : D ≠ 0 := by
      rintro rfl
      apply hD
      rw [coeff_zero_eq_constantCoeff_apply, hf, zero_smul]
    have h1 : D.degree ≤ 1 := by
      by_contra hlt
      push_neg at hlt
      apply hD
      rw [coeff_of_lt_order (lt_of_lt_of_le ?_ (le_order_prod_pow ha D)), smul_zero]
      exact_mod_cast lt_of_le_of_lt hβ hlt
    have h2 : D.degree = 1 := by
      have : D.degree ≠ 0 := fun h => h0 ((Finsupp.degree_eq_zero_iff D).1 h)
      omega
    obtain ⟨s, rfl⟩ := degree_eq_one h2
    simp
  rw [finsum_eq_sum_of_support_subset _ hsupp,
    Finset.sum_image (fun s _ t _ h => Finsupp.single_left_injective one_ne_zero h)]
  refine Finset.sum_congr rfl fun s _ => ?_
  rw [Finsupp.prod_single_index (by simp), pow_one, smul_eq_mul]

end GlobalTheory.QLog.OpLin

namespace GlobalTheory.QLog.OpLin

variable {B O : Type} [CommRing B] [CommRing O] {d : ℕ}

theorem coeff_inl {θ : B →+* O} {μ : Fin d → MvPowerSeries (Fin d ⊕ Fin d) B} (hμ : IsLift θ μ)
    (i j : Fin d) : coeff (Finsupp.single (Sum.inl j) 1) (μ i) = if j = i then 1 else 0 := by
  classical
  have h := congrArg (coeff (Finsupp.single j 1)) (congrFun hμ.unit i)
  simp only [op, at2] at h
  rw [coeff_subst_of_degree_le_one (by rintro (s | s) <;> simp) (hμ.const i)
    (by simp), Fintype.sum_sum_type] at h
  simpa [coeff_X, Finsupp.single_left_inj one_ne_zero, eq_comm] using h

theorem coeff_inr {θ : B →+* O} {μ : Fin d → MvPowerSeries (Fin d ⊕ Fin d) B} (hμ : IsLift θ μ)
    (i j : Fin d) : coeff (Finsupp.single (Sum.inr j) 1) (μ i) = if j = i then 1 else 0 := by
  classical
  have h := congrArg (coeff (Finsupp.single (Sum.inr j) 1)) (hμ.comm i)
  simp only [swap, at2] at h
  rw [coeff_subst_of_degree_le_one (by rintro (s | s) <;> simp) (hμ.const i)
    (by simp), Fintype.sum_sum_type] at h
  rw [← h, ← coeff_inl hμ i j]
  simp [coeff_X, Finsupp.single_left_inj one_ne_zero]

theorem coeff_op_of_degree_le_one : ∀ {B O : Type} [CommRing B] [CommRing O] (θ : B →+* O) {d : ℕ}
    {μ : Fin d → MvPowerSeries (Fin d ⊕ Fin d) B}, IsLift θ μ → ∀ {τ : Type}
    (u v : Fin d → MvPowerSeries τ B), (∀ j, constantCoeff (u j) = 0) →
    (∀ j, constantCoeff (v j) = 0) → ∀ (i : Fin d) (β : τ →₀ ℕ), β.degree ≤ 1 →
    coeff β (op μ u v i) = coeff β (u i + v i) := by
  intro B O _ _ θ d μ hμ τ u v hu hv i β hβ
  classical
  simp only [op, at2]
  rw [coeff_subst_of_degree_le_one (by rintro (s | s); exacts [hu s, hv s]) (hμ.const i) hβ,
    Fintype.sum_sum_type, map_add]
  simp [coeff_inl hμ, coeff_inr hμ]

end GlobalTheory.QLog.OpLin
