# Database Tables and Models with `user_id` Column

This document lists all database tables and corresponding models where the column `user_id` is used.

## Summary

| Table Name | Model Name | Column Name | Usage Context | Notes |
|-----------|------------|-------------|---------------|-------|
| `content_watched` | `ContentWatched` | `user_id` | Foreign Key (references `customers.id`) | **Note:** Despite column name `user_id`, this actually references `customers.id` (customer_id) |
| `user_wallet` | `UserWallet` | `user_id` | Foreign Key (references `customers.id` in DB, but model relates to `User`) | Model has `belongsTo(User)` relationship |
| `wallet_history` | `WalletHistory` | `user_id` | Foreign Key (references `customers.id` in DB, but model relates to `User`) | Model has `belongsTo(User)` relationship |
| `invoice` | `Invoice` | `user_id` | Foreign Key (references `customers.id`) | Column exists in database but not in model's `$fillable` array |
| `payment_mode_saved` | *No Model* | `user_id` | Foreign Key (references `customers.id`) | Table exists but no corresponding Eloquent model found |
| `company_settings` | *No Model* | `user_id` | Foreign Key (references `client_users.id`) | Dynamically created table, no Eloquent model |
| `support_ticket_messages` | `SupportTicketMessage` | `user_id` | Foreign Key (references `client_users.id`) | Model has `belongsTo(ClientUser)` relationship |
| `tbl_customer_subscriptions` | `CustomerSubscription` | `user_id` | Conditional column | Column may or may not exist (checked via `Schema::hasColumn()`) |
| `vouchers` | *No Model* | `issued_to_user_id` | User reference | Similar to `user_id` but with different column name |

---

## Detailed Information

### 1. `content_watched` Table
- **Model:** `App\Models\ContentWatched`
- **Column:** `user_id` (INTEGER)
- **Usage:** Foreign key reference
- **Important Note:** Despite the column name being `user_id`, it actually references `customers.id` (customer_id). The model has both `user()` and `customer()` methods that both return a `belongsTo(Customer)` relationship.
- **Location:** `app/Models/ContentWatched.php`

### 2. `user_wallet` Table
- **Model:** `App\Models\UserWallet`
- **Column:** `user_id` (BIGINT)
- **Usage:** Foreign key reference
- **Relationship:** Model has `belongsTo(ClientUser::class, 'user_id', 'id')` relationship
- **Note:** Database foreign key constraint references `customers.id`, but model relates to `ClientUser` class
- **Location:** `app/Models/UserWallet.php`

### 3. `wallet_history` Table
- **Model:** `App\Models\WalletHistory`
- **Column:** `user_id` (BIGINT)
- **Usage:** Foreign key reference
- **Relationship:** Model has `belongsTo(ClientUser::class, 'user_id', 'id')` relationship
- **Note:** Database foreign key constraint references `customers.id`, but model relates to `ClientUser` class
- **Location:** `app/Models/WalletHistory.php`

### 4. `invoice` Table
- **Model:** `App\Models\Invoice`
- **Column:** `user_id` (BIGINT)
- **Usage:** Foreign key reference (references `customers.id`)
- **Note:** Column exists in database (`mtv_sms_dump.sql`) but is NOT included in the model's `$fillable` array. The model uses `subscriber_id` instead for customer references.
- **Location:** `app/Models/Invoice.php`

### 5. `payment_mode_saved` Table
- **Model:** *No Eloquent model found*
- **Column:** `user_id` (BIGINT)
- **Usage:** Foreign key reference (references `customers.id`)
- **Note:** Table exists in database (`mtv_sms_dump.sql`) but no corresponding Eloquent model was found in the codebase.
- **Database:** Referenced in `mtv_sms_dump.sql`

### 6. `company_settings` Table
- **Model:** *No Eloquent model found*
- **Column:** `user_id` (INTEGER, UNIQUE, NOT NULL)
- **Usage:** Foreign key reference (references `client_users.id`)
- **Note:** Table is dynamically created via SQL statement in `app/Filament/Pages/Account.php`. No Eloquent model exists. Used for storing company/user settings.
- **Location:** Created in `app/Filament/Pages/Account.php` (line ~1002)

### 7. `support_ticket_messages` Table
- **Model:** `App\Models\SupportTicketMessage`
- **Column:** `user_id`
- **Usage:** Foreign key reference (references `client_users.id`)
- **Relationship:** Model has `belongsTo(ClientUser::class, 'user_id')` relationship
- **Location:** `app/Models/SupportTicketMessage.php`

### 8. `tbl_customer_subscriptions` Table
- **Model:** `App\Models\CustomerSubscription`
- **Column:** `user_id` (conditional)
- **Usage:** Conditional column - may or may not exist in the table
- **Note:** Code checks for existence using `Schema::hasColumn('tbl_customer_subscriptions', 'user_id')` before using it. The model primarily uses `subscriber_id` for customer references.
- **Location:** `app/Models/CustomerSubscription.php`, referenced in `app/Http/Controllers/UserAdminActionController.php`

### 9. `vouchers` Table
- **Model:** *No Eloquent model found*
- **Column:** `issued_to_user_id` (similar to `user_id`)
- **Usage:** User reference for voucher assignment
- **Note:** Column name is `issued_to_user_id` rather than `user_id`, but serves similar purpose. Table is accessed via `DB::table('vouchers')` in Livewire components.
- **Location:** Referenced in `app/Livewire/BackOffice/VouchersTable.php` and `app/Livewire/BackOffice/EditVoucherForm.php`

---

## Foreign Key Relationships

Based on database schema (`mtv_sms_dump.sql`):

1. **`invoice.user_id`** → `customers.id`
2. **`payment_mode_saved.user_id`** → `customers.id`
3. **`user_wallet.user_id`** → `customers.id`
4. **`wallet_history.user_id`** → `customers.id`
5. **`company_settings.user_id`** → `client_users.id` (from Account.php)

---

## Model Relationships

### Models with `belongsTo(ClientUser)` relationships:
- `UserWallet::user()` → `ClientUser`
- `WalletHistory::user()` → `ClientUser`
- `SupportTicketMessage::user()` → `ClientUser`

### Models with `belongsTo(Customer)` relationships (using `user_id`):
- `ContentWatched::user()` → `Customer` (via `user_id` column)
- `ContentWatched::customer()` → `Customer` (alias for `user()`)

---

## Important Notes

1. **Naming Inconsistency:** Several tables use `user_id` but actually reference `customers.id` rather than `client_users.id`. This includes:
   - `content_watched.user_id` → `customers.id`
   - `invoice.user_id` → `customers.id`
   - `payment_mode_saved.user_id` → `customers.id`
   - `user_wallet.user_id` → `customers.id` (in DB, but model relates to ClientUser)
   - `wallet_history.user_id` → `customers.id` (in DB, but model relates to ClientUser)

2. **Conditional Columns:** `tbl_customer_subscriptions.user_id` may or may not exist depending on the database schema version.

3. **Dynamic Tables:** `company_settings` table is created dynamically and may not exist in all environments.

4. **Missing Models:** Some tables (`payment_mode_saved`, `company_settings`, `vouchers`) do not have corresponding Eloquent models and are accessed via `DB::table()` or raw queries.

---

## Generated Date
Generated on: 2025-01-XX
Codebase Analysis: Read-only documentation
