-- Migration 014: Sistema Segnalazioni & Risoluzione AI
-- Adattato da alltax.it/docs/sistema-segnalazioni-standard.html
-- Data: 2026-03-10

CREATE TABLE IF NOT EXISTS feedback_reports (
    id              INT AUTO_INCREMENT PRIMARY KEY,
    organization_id INT NOT NULL,
    user_id         INT NOT NULL,
    user_email      VARCHAR(255) NOT NULL,
    user_role       VARCHAR(50)  NULL,
    page_url        VARCHAR(500) NULL,
    tipo            ENUM('bug','ux','funzionalita','domanda','altro') NOT NULL DEFAULT 'bug',
    priorita        ENUM('alta','media','bassa') NOT NULL DEFAULT 'media',
    descrizione     TEXT NOT NULL,
    attachment      LONGTEXT NULL COMMENT 'Base64 screenshot (max ~1.5MB)',
    status          ENUM('aperto','in_lavorazione','risolto','chiuso') NOT NULL DEFAULT 'aperto',
    nota_admin      TEXT NULL,
    ai_categoria    VARCHAR(100) NULL,
    ai_priorita     ENUM('alta','media','bassa') NULL,
    ai_suggerimento TEXT NULL,
    ai_risposta     TEXT NULL COMMENT 'Risposta leggibile per l utente',
    ai_processed    TINYINT(1) NOT NULL DEFAULT 0,
    created_at      DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at      DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    INDEX idx_org_status    (organization_id, status),
    INDEX idx_user          (user_id),
    INDEX idx_ai_processed  (ai_processed, status),

    CONSTRAINT fk_feedback_org  FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE,
    CONSTRAINT fk_feedback_user FOREIGN KEY (user_id)         REFERENCES users(id)         ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
