-- Migration: 2026_05_11_create_consultant_task_review
-- Tabela przechowuje oceny wykonania zadań checklisty wystawiane przez konsultanta.
-- Jedna ocena na (sklep, zadanie, dzień) — unikalny klucz zapewnia upsert ON DUPLICATE KEY.

CREATE TABLE IF NOT EXISTS `consultant_task_review` (
    `id`                  INT UNSIGNED  NOT NULL AUTO_INCREMENT,
    `shop_id`             INT UNSIGNED  NOT NULL,
    `checklist_id`        INT UNSIGNED  NOT NULL,
    `task_id`             INT UNSIGNED  NOT NULL,
    `review_date`         DATE          NOT NULL,
    `completion_id`       INT UNSIGNED  NULL     COMMENT 'FK → todo_task_completion (nullable gdy brak rekordu realizacji)',
    `is_accepted`         TINYINT(1)    NULL     COMMENT '1 = zaakceptowane, 0 = niezaakceptowane, NULL = brak oceny',
    `rating`              TINYINT UNSIGNED NULL  COMMENT 'Ocena jakości 1–5 gwiazdek, NULL = brak oceny',
    `comment`             TEXT          NULL     COMMENT 'Opcjonalny komentarz konsultanta',
    `reviewed_by_user_id` INT UNSIGNED  NULL     COMMENT 'auth_user.id konsultanta wystawiającego ocenę',
    `created_at`          TIMESTAMP     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at`          TIMESTAMP     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uq_ctr_shop_task_date`         (`shop_id`, `task_id`, `review_date`),
    INDEX      `idx_ctr_shop_checklist_date`   (`shop_id`, `checklist_id`, `review_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
