-- 산책 후 피드백 + 찰떡지수(호감도)

-- 1:1 우리 찰떡지수 — 강아지 쌍(정렬: dog_a_id < dog_b_id 로 1행/쌍)
CREATE TABLE dog_affinity (
    id         BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    dog_a_id   BIGINT NOT NULL REFERENCES dogs(id) ON DELETE CASCADE,
    dog_b_id   BIGINT NOT NULL REFERENCES dogs(id) ON DELETE CASCADE,
    score      DOUBLE PRECISION NOT NULL DEFAULT 0,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    CONSTRAINT uq_dog_affinity UNIQUE (dog_a_id, dog_b_id)
);

-- 1:N 동네 찰떡지수 — 강아지별 누적
CREATE TABLE dog_neighborhood (
    dog_id     BIGINT PRIMARY KEY REFERENCES dogs(id) ON DELETE CASCADE,
    score      DOUBLE PRECISION NOT NULL DEFAULT 0,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- 매칭 산책 후 피드백 (매칭당 평가자 1회)
CREATE TABLE match_feedback (
    id           BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    match_id     BIGINT      NOT NULL REFERENCES matching_requests(id) ON DELETE CASCADE,
    rater_dog_id BIGINT      NOT NULL REFERENCES dogs(id) ON DELETE CASCADE,
    rated_dog_id BIGINT      NOT NULL REFERENCES dogs(id) ON DELETE CASCADE,
    verdict      VARCHAR(10) NOT NULL CHECK (verdict IN ('GOOD', 'BAD')),
    reasons      VARCHAR(500),
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
    CONSTRAINT uq_match_feedback UNIQUE (match_id, rater_dog_id)
);
CREATE INDEX idx_feedback_rated ON match_feedback(rated_dog_id);

-- 2시간 후 피드백 알림 1회 발송 표시
ALTER TABLE matching_requests ADD COLUMN feedback_notified BOOLEAN NOT NULL DEFAULT FALSE;