-- V5: Tabel pairing_relations
-- Menyimpan hubungan Guardian <-> User (1:1)
-- Status: PENDING (menunggu User accept), ACTIVE (sudah pair), REJECTED (ditolak/unpair)

CREATE TABLE IF NOT EXISTS pairing_relations (
    id            BIGSERIAL    PRIMARY KEY,
    guardian_id   BIGINT       NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    user_id       BIGINT       NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    status        VARCHAR(20)  NOT NULL DEFAULT 'PENDING',
    invited_at    TIMESTAMP    NOT NULL DEFAULT NOW(),
    responded_at  TIMESTAMP,
    CONSTRAINT uq_guardian_active UNIQUE (guardian_id),
    CONSTRAINT uq_user_active     UNIQUE (user_id)
);

CREATE INDEX IF NOT EXISTS idx_pairing_guardian ON pairing_relations(guardian_id);
CREATE INDEX IF NOT EXISTS idx_pairing_user     ON pairing_relations(user_id);
CREATE INDEX IF NOT EXISTS idx_pairing_status   ON pairing_relations(status);
