# System Update - Quick Reference Guide

## Date
May 8, 2026

## Changes Implemented

### 1. Contact Messages System - FIXED
**Problem:** Database error when submitting contact form - column "full_name" did not exist

**Solution:** 
- Applied migration `007_enhance_contact_messages.sql`
- Renamed `name` → `full_name` in database
- Added: `phone`, `is_read`, `status`, `admin_notes` columns
- Added status constraint and indexes for performance

**Features:**
- ✅ Public contact form with validation
- ✅ Admin dashboard tab with message management
- ✅ Unread counter badge
- ✅ Status tracking (New/Read/Replied)
- ✅ Admin notes for internal use

---

### 2. Flexible Login System - NEW
**Feature:** Users can now login with email, username, OR phone number

**Changes Made:**

#### Backend
- Added `username` column to users table (unique, indexed)
- Made `phone` column unique and indexed for login
- Updated `authenticate_user()` to search by email OR username OR phone
- Changed login endpoint to accept `identifier` instead of `email`
- Updated User model and schemas

#### Frontend
- Login form: "Email" → "Email, Username, or Phone"
- Register form: Added optional username and phone fields
- Updated API calls to use new login format

**Example Logins:**
```
Email:    admin@brandmaster.com
Username: admin123 (if set)
Phone:    0504370045 (if set)
```

---

## Files Modified

### Backend (8 files)
1. ✅ `backend/migrations/007_enhance_contact_messages.sql` - NEW
2. ✅ `backend/migrations/008_add_username_to_users.sql` - NEW
3. ✅ `backend/app/models/user.py` - Added username, updated phone
4. ✅ `backend/app/models/contact_message.py` - Enhanced model
5. ✅ `backend/app/schemas/user.py` - Added username field
6. ✅ `backend/app/schemas/contact.py` - Enhanced schemas
7. ✅ `backend/app/services/auth.py` - Flexible authenticate_user()
8. ✅ `backend/app/routers/auth.py` - Updated login endpoint
9. ✅ `backend/app/routers/contact.py` - Admin endpoints
10. ✅ `backend/app/main.py` - Registered admin router

### Frontend (3 files)
1. ✅ `frontend/src/pages/Login.jsx` - Flexible login form
2. ✅ `frontend/src/pages/Register.jsx` - Added username/phone fields
3. ✅ `frontend/src/pages/Admin.jsx` - Contact Messages tab
4. ✅ `frontend/src/pages/Contact.jsx` - Enhanced form

### Documentation (2 files)
1. ✅ `DATABASE.md` - Updated schema documentation
2. ✅ `deploy-complete-update.bat` - Automated deployment script

---

## Deployment Steps

### Quick Deploy (Automated)
```bash
cd c:\Users\dvirl\OneDrive\Desktop\gitea\brand-master
deploy-complete-update.bat
```

### Manual Deploy
```bash
# 1. Apply migrations
apply-migration.bat 007_enhance_contact_messages.sql
apply-migration.bat 008_add_username_to_users.sql

# 2. Build images
cd backend && docker build -t harbor.dvirlabs.com/my-apps/brand-master-backend:latest .
cd ../frontend && docker build -t harbor.dvirlabs.com/my-apps/brand-master-frontend:latest .

# 3. Push to Harbor
docker push harbor.dvirlabs.com/my-apps/brand-master-backend:latest
docker push harbor.dvirlabs.com/my-apps/brand-master-frontend:latest

# 4. Deploy with Helm
cd brand-master-chart
helm upgrade brand-master . --namespace my-apps --wait
```

---

## Testing Checklist

### ✅ Contact Form Fix
- [ ] Navigate to https://brand-master.dvirlabs.com/contact
- [ ] Fill in all fields (name, email, phone, subject, message)
- [ ] Submit form
- [ ] Verify success message (should NOT get database error)

### ✅ Admin Contact Messages
- [ ] Login as admin
- [ ] Click "Contact Messages" tab
- [ ] Verify unread counter appears
- [ ] Click on message to open details
- [ ] Update status to "Read"
- [ ] Add admin notes
- [ ] Save changes
- [ ] Verify unread counter updates

### ✅ Flexible Login
- [ ] Test login with email: `admin@brandmaster.com`
- [ ] Register new user with username: `testuser123`
- [ ] Test login with username: `testuser123`
- [ ] Register user with phone: `0501234567`
- [ ] Test login with phone: `0501234567`

---

## Database Schema Changes

### contact_message Table
```sql
-- BEFORE
name VARCHAR         -- Changed to full_name
email VARCHAR
subject VARCHAR
message TEXT
created_at TIMESTAMP

-- AFTER
full_name VARCHAR    -- Renamed from 'name'
email VARCHAR
phone VARCHAR        -- NEW (optional)
subject VARCHAR
message TEXT
created_at TIMESTAMP
is_read BOOLEAN      -- NEW (default: false)
status VARCHAR       -- NEW (new/read/replied)
admin_notes TEXT     -- NEW (nullable)
```

### user Table
```sql
-- BEFORE
email VARCHAR (UNIQUE, INDEXED)
phone VARCHAR (NULLABLE)

-- AFTER
email VARCHAR (UNIQUE, INDEXED)
username VARCHAR (UNIQUE, INDEXED, NULLABLE)  -- NEW
phone VARCHAR (UNIQUE, INDEXED, NULLABLE)     -- Now unique & indexed
```

---

## API Changes

### Login Endpoint
```javascript
// BEFORE
POST /api/auth/login
{
  email: "admin@brandmaster.com",
  password: "Admin123!"
}

// AFTER
POST /api/auth/login
{
  identifier: "admin@brandmaster.com",  // Can be email, username, or phone
  password: "Admin123!"
}
```

### Register Endpoint
```javascript
// BEFORE
POST /api/auth/register
{
  email: "user@example.com",
  password: "password123",
  full_name: "John Doe"
}

// AFTER
POST /api/auth/register
{
  email: "user@example.com",
  username: "johndoe",      // NEW (optional)
  phone: "0501234567",      // NEW (optional)
  password: "password123",
  full_name: "John Doe"
}
```

### New Admin Endpoints
```
GET    /api/admin/contact-messages           - List all messages
GET    /api/admin/contact-messages/unread-count - Get unread count
GET    /api/admin/contact-messages/{id}      - Get single message
PUT    /api/admin/contact-messages/{id}      - Update message
DELETE /api/admin/contact-messages/{id}      - Delete message
```

---

## Default Admin Credentials

**Email:** `admin@brandmaster.com`  
**Password:** `Admin123!`

**Can also login with:**
- Email: `admin@brandmaster.com`
- Username: (not set by default)
- Phone: (not set by default)

To add username/phone to admin account:
```sql
UPDATE "user" 
SET username = 'admin', phone = '0504370045' 
WHERE email = 'admin@brandmaster.com';
```

---

## Troubleshooting

### Contact Form Still Shows Error
```bash
# Check if migration was applied
kubectl exec -it -n my-apps deployment/brand-master-postgres -- psql -U brand_master -d brand_master_db

# Run this query
\d contact_message

# Should show: full_name, phone, is_read, status, admin_notes columns
```

### Login with Username Doesn't Work
```bash
# Check if migration was applied
\d "user"

# Should show: username column with UNIQUE constraint

# Check indexes
\di idx_user_username
```

### Backend Logs Show Errors
```bash
kubectl logs -n my-apps deployment/brand-master-backend --tail=50
```

---

## Success Criteria

✅ No database errors when submitting contact form  
✅ Contact messages appear in admin dashboard  
✅ Unread counter shows correct number  
✅ Can login with email  
✅ Can login with username (when set)  
✅ Can login with phone (when set)  
✅ New users can register with username and phone  
✅ All form validations work correctly  

---

## Next Steps (Optional Enhancements)

1. Email notifications for new contact messages
2. Reply to messages from admin panel
3. Set username/phone for existing users via profile page
4. Bulk actions for messages (delete multiple, mark all as read)
5. Export contact messages to CSV
6. Advanced search and filtering

---

**Status:** ✅ Ready for Deployment  
**Deployment Script:** `deploy-complete-update.bat`  
**Estimated Deployment Time:** 5-10 minutes
