# SecureCam database schema

```mermaid
erDiagram
    USERS {
        uuid id PK
        varchar email UK
        varchar name
        varchar password_hash
        boolean email_verified
        text image
        timestamp created_at
        timestamp updated_at
    }

    DEVICES {
        uuid id PK
        uuid user_id FK
        varchar name
        varchar role
        varchar platform
        varchar app_version
        text push_token
        varchar status
        boolean is_camera
        timestamp last_seen_at
        timestamp updated_at
        timestamp created_at
    }

    DEVICE_LINKS {
        uuid id PK
        uuid owner_user_id FK
        uuid camera_device_id FK
        uuid client_device_id FK
        varchar status
        timestamp created_at
        timestamp updated_at
    }

    COMMANDS {
        uuid id PK
        uuid owner_user_id FK
        uuid source_device_id FK
        uuid target_device_id FK
        varchar command_type
        jsonb payload
        varchar status
        integer retry_count
        timestamp last_dispatched_at
        timestamp acknowledged_at
        text error
        timestamp created_at
        timestamp updated_at
    }

    STREAM_SESSIONS {
        uuid id PK
        uuid owner_user_id FK
        uuid camera_device_id FK
        uuid requester_device_id FK
        varchar status
        varchar reason
        varchar media_provider
        varchar media_session_id
        text publish_endpoint
        text subscribe_endpoint
        varchar stream_key
        timestamp started_at
        timestamp ended_at
        jsonb metadata
        timestamp created_at
        timestamp updated_at
    }

    RECORDINGS {
        uuid id PK
        uuid owner_user_id FK
        uuid stream_session_id FK
        uuid camera_device_id FK
        uuid requester_device_id FK
        uuid event_id FK
        varchar object_key
        varchar bucket
        integer duration_seconds
        integer size_bytes
        varchar status
        timestamp available_at
        text error
        timestamp created_at
        timestamp updated_at
    }

    EVENTS {
        uuid id PK
        uuid user_id FK
        uuid device_id FK
        varchar title
        varchar triggered_by
        varchar status
        timestamp started_at
        timestamp ended_at
        timestamp notified_at
        varchar video_url UK
        timestamp created_at
        timestamp updated_at
    }

    NOTIFICATIONS {
        uuid id PK
        uuid event_id FK
        uuid user_id FK
        timestamp sent_at
        varchar channel
        varchar status
        boolean is_read
    }

    NOTIFICATION_DELIVERIES {
        uuid id PK
        uuid owner_user_id FK
        uuid recipient_device_id FK
        varchar type
        jsonb payload
        varchar status
        integer attempts
        text last_error
        timestamp sent_at
        timestamp next_attempt_at
        timestamp created_at
        timestamp updated_at
    }

    AUDIT_LOGS {
        uuid id PK
        uuid owner_user_id FK
        uuid actor_device_id FK
        varchar action
        varchar target_type
        varchar target_id
        jsonb metadata
        text ip_address
        timestamp created_at
    }

    ACCOUNT {
        uuid id PK
        uuid user_id FK
        text account_id
        text provider_id
        text access_token
        text refresh_token
        timestamp access_token_expires_at
        timestamp refresh_token_expires_at
        text id_token
        text scope
        text password
        timestamp created_at
        timestamp updated_at
    }

    SESSION {
        uuid id PK
        text token UK
        uuid user_id FK
        timestamp expires_at
        text ip_address
        text user_agent
        timestamp created_at
        timestamp updated_at
    }

    VERIFICATION {
        uuid id PK
        text identifier
        text value
        timestamp expires_at
        timestamp created_at
        timestamp updated_at
    }

    USERS ||--o{ DEVICES : owns
    USERS ||--o{ DEVICE_LINKS : owns
    USERS ||--o{ COMMANDS : owns
    USERS ||--o{ STREAM_SESSIONS : owns
    USERS ||--o{ RECORDINGS : owns
    USERS ||--o{ EVENTS : creates
    USERS ||--o{ NOTIFICATIONS : receives
    USERS ||--o{ NOTIFICATION_DELIVERIES : owns
    USERS ||--o{ AUDIT_LOGS : owns
    USERS ||--o{ ACCOUNT : auth_account
    USERS ||--o{ SESSION : auth_session

    DEVICES ||--o{ DEVICE_LINKS : camera_device
    DEVICES ||--o{ DEVICE_LINKS : client_device
    DEVICES ||--o{ COMMANDS : source_device
    DEVICES ||--o{ COMMANDS : target_device
    DEVICES ||--o{ STREAM_SESSIONS : camera_device
    DEVICES ||--o{ STREAM_SESSIONS : requester_device
    DEVICES ||--o{ RECORDINGS : camera_device
    DEVICES ||--o{ RECORDINGS : requester_device
    DEVICES ||--o{ EVENTS : triggers
    DEVICES ||--o{ NOTIFICATION_DELIVERIES : recipient_device
    DEVICES ||--o{ AUDIT_LOGS : actor_device

    STREAM_SESSIONS ||--o{ RECORDINGS : produces
    EVENTS o|--o{ RECORDINGS : may_attach
    EVENTS ||--o{ NOTIFICATIONS : generates
```

## Notes

- This is generated from the current Drizzle schema in [schema.ts](/home/matiss/Documents/Final Year Project/Backend/db/schema.ts).
- `VERIFICATION` is currently standalone in the schema and does not reference `USERS`.
- `DEVICE_LINKS`, `COMMANDS`, `STREAM_SESSIONS`, and `RECORDINGS` each reference `DEVICES` more than once for different roles.
- `ACCOUNT`, `SESSION`, and `VERIFICATION` are the Better Auth support tables.
- `recordings` now absorbs the old upload metadata responsibility that previously lived in `videos`.
- `notification_deliveries` represents push delivery jobs, while `notifications` remains the user-facing event notification history.
