-- #migration 2026_09_21_001
-- Ajoute la quantité aux commandes (achat en lot d'une pièce existante, ex:
-- un restaurateur qui commande 30 couteaux) et une table de demandes de
-- devis pour les besoins qui ne rentrent pas dans un prix unitaire x
-- quantité (ex: un architecte qui commande une série sur-mesure) — voir
-- CDC page-prevente-vente-artisan.

ALTER TABLE artisan_prevente_commandes
  ADD COLUMN quantite INT UNSIGNED NOT NULL DEFAULT 1 AFTER piece_id;

CREATE TABLE IF NOT EXISTS artisan_prevente_devis (
  id                VARCHAR(40)  NOT NULL PRIMARY KEY,
  page_id           VARCHAR(40)  NOT NULL,
  piece_id          VARCHAR(40)  NULL,       -- pièce de référence si applicable, NULL si demande hors catalogue
  demandeur_nom     VARCHAR(150) NOT NULL DEFAULT '',
  demandeur_email   VARCHAR(190) NOT NULL,
  quantite_estimee  INT UNSIGNED NULL,
  message           TEXT NULL,
  statut            ENUM('nouvelle','traitee','sans_suite') NOT NULL DEFAULT 'nouvelle',
  created_at        DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (page_id) REFERENCES artisan_prevente_pages(id) ON DELETE CASCADE,
  FOREIGN KEY (piece_id) REFERENCES artisan_prevente_pieces(id) ON DELETE SET NULL,
  INDEX idx_pd_page (page_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
