# Database Schema Documentation

Database: `document_authentication` (MySQL / utf8mb4)

---

## Entity Relationship Overview

```
users (1) ────< documents (N) ────< document_validations (N)
                    │
                    ├────(1:1) document_certificates (1) ────< verification_logs (N)
                    │
                    ├────< document_events (N)
                    └────< notifications (N)

document_types (1) ────< documents (N)
users (1) ────< audit_logs (N)
```

---

## Tables

### 1. `users`
| Column | Type | Constraints / Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK, Auto-increment |
| `name` | VARCHAR(255) | User's full name |
| `email` | VARCHAR(191) | Unique |
| `phone` | VARCHAR(255) | Nullable |
| `password` | VARCHAR(255) | Bcrypt hash |
| `role` | ENUM | `'client'`, `'admin'`, `'super_admin'` |
| `status` | ENUM | `'active'`, `'inactive'`, `'suspended'` |
| `email_verified_at`| TIMESTAMP | Nullable |
| `remember_token` | VARCHAR(100) | |
| `created_at`, `updated_at`, `deleted_at` | TIMESTAMP | Soft deletes |

### 2. `document_types`
| Column | Type | Constraints / Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK, Auto-increment |
| `name` | VARCHAR(255) | e.g. "Bank Statement", "Identity Document" |
| `description` | TEXT | Nullable |
| `is_active` | BOOLEAN | Default `true` |
| `created_at`, `updated_at` | TIMESTAMP | |

### 3. `documents`
| Column | Type | Constraints / Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK, Auto-increment |
| `user_id` | BIGINT UNSIGNED | FK -> `users.id` (RESTRICT) |
| `document_type_id` | BIGINT UNSIGNED | FK -> `document_types.id` (RESTRICT) |
| `title` | VARCHAR(255) | User-entered document title |
| `original_filename`| VARCHAR(255) | Original uploaded file name |
| `original_path` | VARCHAR(255) | Private disk storage path |
| `original_mime_type`| VARCHAR(255) | Validated MIME type |
| `original_hash` | VARCHAR(64) | SHA-256 hash of original file |
| `status` | ENUM | `'pending'`, `'under_review'`, `'approved'`, `'rejected'`, `'revoked'` |
| `submitted_at` | TIMESTAMP | Submission timestamp |
| `created_at`, `updated_at`, `deleted_at` | TIMESTAMP | Soft deletes |

### 4. `document_validations`
| Column | Type | Constraints / Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK, Auto-increment |
| `document_id` | BIGINT UNSIGNED | FK -> `documents.id` (CASCADE) |
| `validator_id` | BIGINT UNSIGNED | FK -> `users.id` (RESTRICT) |
| `decision` | ENUM | `'approved'`, `'rejected'` |
| `comments` | TEXT | Mandatory for rejection, optional for approval |
| `validated_at` | TIMESTAMP | |
| `created_at`, `updated_at` | TIMESTAMP | |

### 5. `document_certificates`
| Column | Type | Constraints / Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK, Auto-increment |
| `document_id` | BIGINT UNSIGNED | Unique FK -> `documents.id` (RESTRICT) |
| `validation_code` | VARCHAR(255) | Unique, e.g. `DOC-2026-XXXXXXXX` |
| `verification_token`| VARCHAR(64) | Unique, 64-character cryptographically random hex |
| `qr_code_path` | VARCHAR(255) | Storage path of QR PNG |
| `barcode_path` | VARCHAR(255) | Nullable |
| `authenticated_file_path` | VARCHAR(255) | Storage path of final authenticated PDF |
| `authenticated_hash`| VARCHAR(64) | SHA-256 hash of authenticated PDF |
| `issued_at` | TIMESTAMP | |
| `expires_at` | TIMESTAMP | Nullable |
| `revoked_at` | TIMESTAMP | Nullable |
| `revocation_reason` | TEXT | Nullable |
| `revoked_by` | BIGINT UNSIGNED | Nullable FK -> `users.id` |
| `created_at`, `updated_at` | TIMESTAMP | |

### 6. `document_events`
| Column | Type | Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK |
| `document_id` | BIGINT UNSIGNED | FK -> `documents.id` (CASCADE) |
| `user_id` | BIGINT UNSIGNED | Nullable FK -> `users.id` |
| `event_type` | VARCHAR(255) | `'document.submitted'`, `'document.approved'`, etc. |
| `description` | TEXT | Human-readable log entry |
| `metadata` | JSON | Nullable structured data |
| `created_at` | TIMESTAMP | |

### 7. `verification_logs`
| Column | Type | Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK |
| `certificate_id` | BIGINT UNSIGNED | Nullable FK -> `document_certificates.id` |
| `verification_result`| ENUM | `'valid'`, `'invalid'`, `'revoked'`, `'not_found'`, `'hash_match'`, `'hash_no_match'` |
| `verification_method`| ENUM | `'qr_scan'`, `'token_url'`, `'file_hash'` |
| `ip_address` | VARCHAR(45) | Client IP address |
| `user_agent` | TEXT | Browser / scanner user-agent string |
| `metadata` | JSON | Nullable |
| `verified_at` | TIMESTAMP | |

### 8. `notifications`
| Column | Type | Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK |
| `user_id` | BIGINT UNSIGNED | FK -> `users.id` (CASCADE) |
| `document_id` | BIGINT UNSIGNED | Nullable FK -> `documents.id` |
| `title` | VARCHAR(255) | Notification title |
| `message` | TEXT | Notification body |
| `type` | VARCHAR(50) | `'info'`, `'success'`, `'warning'`, `'error'` |
| `read_at` | TIMESTAMP | Nullable |
| `created_at`, `updated_at` | TIMESTAMP | |

### 9. `audit_logs`
| Column | Type | Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK |
| `user_id` | BIGINT UNSIGNED | Nullable FK -> `users.id` |
| `action` | VARCHAR(255) | e.g. `'certificate.issued'`, `'client.status_toggled'` |
| `entity_type` | VARCHAR(255) | e.g. `'Document'`, `'DocumentCertificate'` |
| `entity_id` | BIGINT UNSIGNED | Target record ID |
| `ip_address` | VARCHAR(45) | Actor IP address |
| `details` | JSON | Structured audit parameters |
| `created_at` | TIMESTAMP | |

### 10. `system_settings`
| Column | Type | Description |
|---|---|---|
| `id` | BIGINT UNSIGNED | PK |
| `setting_key` | VARCHAR(191) | Unique key |
| `setting_value` | TEXT | Nullable |
| `description` | VARCHAR(255) | Setting description |
| `created_at`, `updated_at` | TIMESTAMP | |

