-- sent_notification_tracker: Prevent duplicate notifications for schedules
CREATE TABLE sent_notification_tracker (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  schedule_id UUID NOT NULL,
  notification_date DATE NOT NULL,
  reminder_level INTEGER NOT NULL CHECK (reminder_level IN (1, 2, 3)),
  delivery_method VARCHAR(20) NOT NULL CHECK (delivery_method IN ('email', 'in_app', 'both')),
  created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,

  CONSTRAINT fk_sent_notification_schedule
    FOREIGN KEY (schedule_id)
    REFERENCES maintenance_schedules(id)
    ON DELETE CASCADE,

  CONSTRAINT unique_notification_per_schedule_date_level
    UNIQUE (schedule_id, notification_date, reminder_level)
);

-- Indexes for performance
CREATE INDEX idx_sent_notification_tracker_schedule_id ON sent_notification_tracker(schedule_id);
CREATE INDEX idx_sent_notification_tracker_notification_date ON sent_notification_tracker(notification_date);
