# ShopCall.ai Refactoring TODO

## Project Goal
Refactor the entire project to remove Vercel dependencies and enable static hosting on simple webhosting. Use Supabase Edge Functions for backend functionality.

---

## Phase 1: Analysis & Planning

- [x] Analyze current backend API endpoints in `shopcall.ai-backend-main/api/index.js`
  - [x] Document all authentication endpoints (signup, login, OTP, logout)
  - [x] Document OAuth flows (Shopify, WooCommerce)
  - [x] Document GDPR webhook endpoints
  - [x] Document helper functions (generateOTP, sendOTPEmail, etc.)
- [x] Identify current dependencies and integrations
  - [x] Nodemailer for email sending → Replaced with Resend
  - [x] In-memory stores (pendingSignups, nonceStore) → Replaced with database tables
  - [x] Supabase client usage patterns
- [x] Design Edge Functions architecture
  - [x] Plan function structure and organization
  - [x] Plan shared utilities and dependencies

---

## Phase 2: Database Migration

- [x] Create Supabase database tables to replace in-memory stores
  - [x] Create `pending_signups` table (replace pendingSignups Map)
    - Fields: signup_id, email, password, full_name, company_name, user_name, otp, created_at, expires_at
  - [x] Create `oauth_nonces` table (replace nonceStore Map)
    - Fields: nonce, user_id, platform, shop, app_url, shopname, created_at, expires_at
  - [x] Add TTL/cleanup mechanism for expired records (cleanup functions created)
- [x] Update existing `stores` table if needed (already has required fields)
- [x] Create database migration files (applied via Supabase MCP)
- [ ] Test database schema with sample data

---

## Phase 3: Supabase Edge Functions Setup

- [x] Set up Supabase Edge Functions project structure
  - [x] Used Supabase MCP tool for deployment
  - [x] Created modular function structure
  - [x] Shared utilities (cors.ts, email.ts, oauth.ts)
- [x] Create Edge Function for authentication endpoints
  - [x] `/auth/signup` - Store user data and send OTP
  - [x] `/auth/signup/verify` - Verify OTP and create user
  - [x] `/auth/signup/resend-otp` - Resend OTP email
  - [x] `/auth/login` - Email/password login
  - [x] `/auth/logout` - Sign out
  - [x] `/auth/check` - Validate session token
- [x] Create Edge Function for Shopify integration
  - [x] `/shopify-oauth/init` - Initialize OAuth flow
  - [x] `/shopify-oauth/callback` - Handle OAuth callback
- [x] Create Edge Function for WooCommerce integration
  - [x] `/woocommerce-oauth/init` - Initialize OAuth flow (requires auth)
  - [x] `/woocommerce-oauth/callback` - Handle OAuth callback
- [x] Create Edge Function for GDPR webhooks
  - [x] `/gdpr-webhooks/customers-data-request` - Customer data request
  - [x] `/gdpr-webhooks/customers-redact` - Customer redaction
  - [x] `/gdpr-webhooks/shop-redact` - Shop redaction
- [x] Implement email sending in Edge Functions
  - [x] Research Edge Function email options → Chose Resend
  - [x] Implement OTP email templates
  - [x] Replace Nodemailer functionality
- [x] Create shared utilities for Edge Functions
  - [x] OTP generation and validation (email.ts)
  - [x] Email templates and sending (email.ts)
  - [x] URL normalization and validation (oauth.ts)
  - [x] HMAC webhook verification (in gdpr-webhooks)
  - [x] CORS headers (cors.ts)

---

## Phase 4: Frontend Refactoring

- [x] Update frontend to use environment variables
  - [x] Create `.env.example` file
  - [x] Replace hardcoded backend URL (https://shopcall-ai-backend.vercel.app)
  - [x] Add Supabase Edge Function base URL configuration
- [x] Update API calls to use Edge Functions
  - [x] Update AuthContext to call Edge Functions
  - [x] Update all API endpoints in components (6 files updated)
  - [ ] Test authentication flow
- [x] Configure Vite for static build
  - [x] Review vite.config.ts for static hosting (already configured)
  - [x] Proper base path configuration exists
  - [x] Build output directory configured (dist/)
- [x] Add client-side routing support for static hosting
  - [x] Create .htaccess for Apache hosting
  - [x] Create nginx.conf example for Nginx hosting
  - [x] Handle 404 fallback to index.html
- [ ] Remove Vercel dependencies (optional - can keep for now)
  - [ ] Delete vercel.json from frontend
  - [ ] Remove any Vercel-specific code
- [x] Update environment handling
  - [x] Use Vite's import.meta.env
  - [x] Centralized in src/lib/config.ts

---

## Phase 5: Backend Cleanup

- [ ] Remove Vercel-specific files from backend
  - [ ] Delete vercel.json
  - [ ] Archive api/index.js (keep for reference)
- [ ] Move backend code to Edge Functions
  - [ ] Ensure all functionality is migrated
  - [ ] Delete unused files
- [ ] Update .gitignore for both projects
  - [ ] Ensure proper ignoring of build artifacts
  - [ ] Ignore environment files
  - [ ] Ignore Supabase local dev files

---

## Phase 6: Deployment & Testing

- [ ] Deploy Edge Functions to Supabase
  - [ ] Configure Edge Function secrets
  - [ ] Set up environment variables in Supabase
  - [ ] Deploy each function
  - [ ] Test Edge Function endpoints
- [ ] Build frontend for static hosting
  - [ ] Run production build (npm run build)
  - [ ] Test build output locally
  - [ ] Verify all routes work correctly
  - [ ] Test with different hosting scenarios
- [ ] End-to-end testing
  - [ ] Test authentication flow (signup, OTP, login)
  - [ ] Test Shopify OAuth integration
  - [ ] Test WooCommerce OAuth integration
  - [ ] Test dashboard and protected routes
  - [ ] Test GDPR webhook endpoints
- [ ] Performance testing
  - [ ] Test Edge Function response times
  - [ ] Test static hosting performance
  - [ ] Optimize bundle size if needed

---

## Phase 7: Documentation

- [x] Create deployment guide for static hosting
  - [x] Apache hosting setup with .htaccess
  - [x] Nginx hosting setup with config
  - [x] Environment variable configuration
  - [x] Edge Functions deployment steps
- [x] Create DEPLOYMENT_GUIDE.md
  - [x] Document new Edge Functions architecture
  - [x] Deployment instructions for multiple hosting options
  - [x] Environment variable configuration
  - [x] Testing procedures
  - [x] Troubleshooting section
- [x] Create REFACTORING_SUMMARY.md
  - [x] Complete overview of all changes
  - [x] What was accomplished
  - [x] What remains to be done
  - [x] Migration checklist
- [ ] Update CLAUDE.md (optional)
  - [ ] Document new Edge Functions architecture
  - [ ] Update deployment instructions
  - [ ] Remove Vercel-specific information
  - [ ] Add static hosting requirements

---

## Phase 8: Final Review

- [ ] Code review and cleanup
  - [ ] Remove unused dependencies
  - [ ] Clean up commented code
  - [ ] Ensure consistent code style
- [ ] Security review
  - [ ] Verify no secrets in code
  - [ ] Check CORS configuration
  - [ ] Verify authentication flows
  - [ ] Test Edge Function security
- [ ] Final testing
  - [ ] Complete end-to-end test
  - [ ] Test on actual static hosting
  - [ ] Verify all features work
- [ ] Git repository cleanup
  - [ ] Commit all changes
  - [ ] Create proper commit messages
  - [ ] Tag release version

---

## Notes

### Current Architecture
- **Frontend**: React/Vite at `shopcall.ai-main/` (deployed to Vercel)
- **Backend**: Express.js serverless function at `shopcall.ai-backend-main/api/index.js` (deployed to Vercel)
- **Database**: Supabase PostgreSQL
- **Authentication**: Supabase Auth

### Target Architecture
- **Frontend**: Static React build (hosted on simple webhosting)
- **Backend**: Supabase Edge Functions (Deno runtime)
- **Database**: Supabase PostgreSQL (unchanged)
- **Authentication**: Supabase Auth (unchanged)

### Key Considerations
- Edge Functions use Deno runtime (not Node.js)
- Need to replace Nodemailer with Deno-compatible email solution
- In-memory stores must move to Supabase database
- Frontend needs proper routing configuration for static hosting
- Environment variables must be properly managed for static hosting

### Dependencies to Replace
- ❌ Vercel serverless functions → ✅ Supabase Edge Functions
- ❌ Nodemailer → ✅ Deno-compatible email service (Resend/SendGrid)
- ❌ In-memory Maps → ✅ Supabase database tables
- ❌ Hardcoded URLs → ✅ Environment variables

---

## Progress Tracking

**Status**: ✅ Core Implementation Complete - Testing Phase
**Last Updated**: 2025-10-27
**Current Phase**: Phase 6 - Deployment & Testing

### Summary of Completed Work

- ✅ Phase 1: Analysis & Planning - **COMPLETE**
- ✅ Phase 2: Database Migration - **COMPLETE** (testing pending)
- ✅ Phase 3: Edge Functions Setup - **COMPLETE**
- ✅ Phase 4: Frontend Refactoring - **COMPLETE** (testing pending)
- ⏭️ Phase 5: Backend Cleanup - **SKIPPED** (can keep old code for reference)
- 🔄 Phase 6: Deployment & Testing - **IN PROGRESS**
- ✅ Phase 7: Documentation - **COMPLETE**
- ⏸️ Phase 8: Final Review - **PENDING**

### What's Ready
- Database tables created and configured
- 4 Edge Functions deployed (auth, shopify-oauth, woocommerce-oauth, gdpr-webhooks)
- Frontend updated to use Edge Functions
- Static hosting configuration files created
- Comprehensive documentation written

### What Needs Testing
- [ ] Complete authentication flow (signup → OTP → verify → login)
- [ ] OAuth integrations (Shopify, WooCommerce)
- [ ] Email delivery (Resend API needs to be configured)
- [ ] Static hosting routing
- [ ] GDPR webhooks

### Next Steps
1. Configure Resend API key in Supabase
2. Test authentication flow end-to-end
3. Test OAuth integrations
4. Deploy to production hosting
5. Update OAuth callback URLs in provider settings
