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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# UI API Integration Prompt Instructions

## 🎯 **System Overview**
You are building a UI for a hierarchical B2B beauty supply chain management system. The backend provides REST APIs for managing merchants in a 4-level hierarchy: **Parent Company β†’ cnf β†’ distributors β†’ retails**.

## πŸ—οΈ **API Base Configuration**

### Base URL & Authentication
```javascript
const API_BASE_URL = 'http://localhost:8000'
const API_ENDPOINTS = {
  merchants: '/merchants',
  merchantSettings: '/merchant-settings',
  users: '/system-users',
  roles: '/access-roles',
  auth: '/auth'
}

// JWT Token Authentication (implement as needed)
const apiHeaders = {
  'Content-Type': 'application/json',
  'Authorization': `Bearer ${localStorage.getItem('authToken')}`
}
```

### Test Credentials for Development
```javascript
const TEST_CREDENTIALS = {
  superAdmin: { username: 'superadmin', password: 'SuperAdmin@123' },
  companyAdmin: { username: 'companyadmin', password: 'CompanyAdmin@123' },
  cnfManager: { username: 'cnfmanager_north', password: 'cnfManager@123' },
  distManager: { username: 'distmanager_delhi', password: 'DistManager@123' },
  retailOwner: { username: 'owner_cp_luxury', password: 'retailOwner@123' }
}
```

## πŸ“± **Core UI Components to Build**

### 1. **Merchant Hierarchy Tree View**
```javascript
// GET /merchants - Fetch all merchants
const fetchMerchantsHierarchy = async () => {
  const response = await fetch(`${API_BASE_URL}/merchants`, {
    headers: apiHeaders
  })
  const merchants = await response.json()
  
  // Build tree structure
  return buildHierarchyTree(merchants)
}

// Expected merchant object structure:
const merchantStructure = {
  merchant_id: "string",
  merchant_name: "string", 
  merchant_type: "parent_company|cnf|distributor|retail",
  merchant_code: "string",
  parent_merchant_id: "string|null",
  status: "active|inactive",
  contact: {
    phone: "string",
    email: "string", 
    address_line1: "string",
    city: "string",
    state: "string",
    pincode: "string"
  },
  kyc: { /* GST, PAN details */ }
}
```

### 2. **Merchant Management Dashboard**
Create these UI sections:

#### **A. Merchant List/Grid View**
```javascript
// API Calls:
// GET /merchants?merchant_type=retail - Filter by type
// GET /merchants?status=active - Filter by status  
// GET /merchants/children/{parent_id} - Get child merchants

const merchantListConfig = {
  columns: [
    { key: 'merchant_code', label: 'Code', sortable: true },
    { key: 'merchant_name', label: 'Name', sortable: true },
    { key: 'merchant_type', label: 'Type', filterable: true },
    { key: 'contact.city', label: 'City' },
    { key: 'status', label: 'Status', filterable: true },
    { key: 'actions', label: 'Actions' }
  ],
  filters: ['merchant_type', 'status', 'city'],
  actions: ['view', 'edit', 'settings', 'delete']
}
```

#### **B. Merchant Creation Form**
```javascript
// POST /merchants - Create new merchant
const createMerchantForm = {
  fields: {
    merchant_name: { type: 'text', required: true },
    merchant_type: { 
      type: 'select', 
      options: ['cnf', 'distributor', 'retail'],
      required: true 
    },
    parent_merchant_id: { 
      type: 'autocomplete',
      source: '/merchants', // Dynamic based on type
      required: true 
    },
    contact: {
      phone: { type: 'tel', required: true },
      email: { type: 'email', required: true },
      address_line1: { type: 'text', required: true },
      city: { type: 'text', required: true },
      state: { type: 'select', options: 'INDIAN_STATES' },
      pincode: { type: 'text', pattern: '[0-9]{6}' }
    },
    kyc: {
      gst_number: { type: 'text', pattern: 'GST_REGEX' },
      pan_number: { type: 'text', pattern: 'PAN_REGEX' }
    }
  },
  validation: 'client-side + server-side'
}
```

### 3. **Merchant Settings Management**
```javascript
// GET /merchant-settings/{merchant_id} - Fetch settings
// PUT /merchant-settings/{merchant_id} - Update settings

const merchantSettingsForm = {
  businessSettings: {
    auto_allocate_stock: { type: 'toggle' },
    credit_limit: { type: 'currency', min: 0 },
    payment_terms_days: { type: 'number', min: 0, max: 365 }
  },
  inventorySettings: {
    low_stock_threshold: { type: 'number', min: 1 },
    enable_backorder: { type: 'toggle' },
    auto_reorder: { type: 'toggle' }
  },
  notifications: {
    email_notifications: { type: 'toggle' },
    sms_notifications: { type: 'toggle' },
    low_stock_alerts: { type: 'toggle' }
  },
  operatingHours: {
    // Dynamic day-wise hour picker
    format: { open_time: 'HH:mm', close_time: 'HH:mm', is_open: boolean }
  },
  pricing: {
    default_markup_percentage: { type: 'percentage', min: 0, max: 100 },
    allow_discount: { type: 'toggle' },
    max_discount_percentage: { type: 'percentage', min: 0, max: 50 }
  },
  taxSettings: {
    gst_rate: { type: 'percentage', default: 18 },
    inclusive_pricing: { type: 'toggle' },
    tax_exemption: { type: 'toggle' }
  }
}
```

### 4. **User & Role Management**
```javascript
// GET /access-roles - Fetch all roles
// GET /system-users - Fetch all users
// POST /system-users - Create user

const userManagementConfig = {
  userForm: {
    username: { type: 'text', unique: true },
    email: { type: 'email', unique: true },
    full_name: { type: 'text' },
    role_id: { 
      type: 'select',
      source: '/access-roles',
      displayField: 'role_name'
    },
    merchant_id: {
      type: 'autocomplete', 
      source: '/merchants',
      filter: 'based on role permissions'
    },
    password: { type: 'password', minLength: 8 }
  },
  roleBasedAccess: {
    // Show/hide features based on user role
    'role_super_admin': 'ALL_FEATURES',
    'role_company_admin': 'EXCLUDE_DELETE_OPERATIONS', 
    'role_cnf_manager': 'REGIONAL_SCOPE_ONLY',
    'role_distributor_manager': 'LOCAL_SCOPE_ONLY',
    'role_retail_owner': 'OWN_MERCHANT_ONLY'
  }
}
```

## πŸ”„ **Key API Integration Patterns**

### 1. **Hierarchical Data Loading**
```javascript
// Pattern: Load parent first, then children on-demand
const loadHierarchicalData = async (parentId = null) => {
  if (!parentId) {
    // Load root level (Parent Company + cnfs)
    return await fetch(`${API_BASE_URL}/merchants?merchant_type=parent_company,cnf`)
  } else {
    // Load children of specific parent
    return await fetch(`${API_BASE_URL}/merchants/children/${parentId}`)
  }
}

// Tree expansion pattern
const expandTreeNode = async (nodeId) => {
  const children = await fetch(`${API_BASE_URL}/merchants/children/${nodeId}`)
  // Update tree state with children
}
```

### 2. **Context-Aware Form Fields**
```javascript
// Parent selection based on merchant type
const getParentOptions = async (merchantType) => {
  const parentTypes = {
    'cnf': ['parent_company'],
    'distributor': ['cnf'], 
    'retail': ['distributor']
  }
  
  const parentType = parentTypes[merchantType]?.[0]
  if (parentType) {
    return await fetch(`${API_BASE_URL}/merchants?merchant_type=${parentType}`)
  }
}

// Settings defaults based on merchant type
const getSettingsDefaults = (merchantType) => {
  const defaults = {
    'cnf': { credit_limit: 10000000, payment_terms_days: 90, markup: 5 },
    'distributor': { credit_limit: 3000000, payment_terms_days: 60, markup: 15 },
    'retail': { credit_limit: 500000, payment_terms_days: 30, markup: 30 }
  }
  return defaults[merchantType] || {}
}
```

### 3. **Error Handling & Validation**
```javascript
const apiErrorHandler = (error) => {
  const errorMessages = {
    400: 'Invalid request data. Please check your input.',
    401: 'Authentication required. Please login.',
    403: 'You do not have permission for this action.',
    404: 'Resource not found.',
    409: 'Duplicate entry. This record already exists.',
    422: 'Validation failed. Please check highlighted fields.',
    500: 'Server error. Please try again later.'
  }
  
  return errorMessages[error.status] || 'An unexpected error occurred.'
}

// Form validation
const validateMerchantForm = (data) => {
  const errors = {}
  
  if (!data.merchant_name?.trim()) {
    errors.merchant_name = 'Merchant name is required'
  }
  
  if (data.merchant_type === 'retail' && !data.parent_merchant_id) {
    errors.parent_merchant_id = 'Parent distributor is required for retails'
  }
  
  // GST validation for Indian businesses
  if (data.kyc?.gst_number && !/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/.test(data.kyc.gst_number)) {
    errors['kyc.gst_number'] = 'Invalid GST number format'
  }
  
  return errors
}
```

## 🎨 **UI/UX Implementation Guidelines**

### 1. **Responsive Design Requirements**
```css
/* Mobile-first approach for merchant management */
.merchant-card {
  /* Stack vertically on mobile */
  @media (max-width: 768px) {
    flex-direction: column;
  }
}

.merchant-hierarchy-tree {
  /* Horizontal scroll on mobile */
  @media (max-width: 768px) {
    overflow-x: auto;
    white-space: nowrap;
  }
}
```

### 2. **State Management Pattern**
```javascript
// Redux/Zustand store structure
const merchantStore = {
  state: {
    merchants: [],
    selectedMerchant: null,
    hierarchyTree: {},
    filters: { type: 'all', status: 'active' },
    loading: false,
    error: null
  },
  actions: {
    fetchMerchants: async (filters) => { },
    createMerchant: async (data) => { },
    updateMerchant: async (id, data) => { },
    deleteMerchant: async (id) => { },
    setSelectedMerchant: (merchant) => { },
    setFilters: (filters) => { }
  }
}
```

### 3. **Component Architecture**
```javascript
// Component hierarchy
const ComponentStructure = {
  'MerchantDashboard': {
    'MerchantHierarchyTree': ['TreeNode', 'TreeNodeActions'],
    'MerchantListView': ['MerchantCard', 'FilterBar', 'Pagination'],
    'MerchantFormModal': ['FormFields', 'ValidationErrors'],
    'MerchantSettingsPanel': ['SettingsSections', 'OperatingHours']
  },
  'UserManagement': {
    'UserListView': ['UserCard', 'RoleFilter'],
    'UserFormModal': ['UserForm', 'RoleSelector'],
    'PermissionsMatrix': ['RolePermissions']
  }
}
```

## πŸ“‹ **Testing Checklist**

### 1. **API Integration Tests**
- [ ] Fetch merchants hierarchy successfully
- [ ] Create merchant with valid parent relationship
- [ ] Update merchant settings with validation
- [ ] Handle API errors gracefully
- [ ] Implement proper loading states

### 2. **UI Functionality Tests**
- [ ] Tree view expands/collapses correctly
- [ ] Form validation works client-side
- [ ] Filter/search functionality
- [ ] Responsive design on mobile
- [ ] Role-based feature visibility

### 3. **Business Logic Tests**
- [ ] cnf can only have Parent Company as parent
- [ ] distributor can only have cnf as parent
- [ ] retail can only have distributor as parent
- [ ] Credit limits are enforced based on merchant type
- [ ] Operating hours format validation

## πŸš€ **Development Workflow**

### 1. **Setup & Configuration**
```bash
# Start the API server
uvicorn app.main:app --reload --port 8000

# Access API documentation
open http://localhost:8000/docs

# Test API endpoints
curl -X GET "http://localhost:8000/merchants" -H "Content-Type: application/json"
```

### 2. **Implementation Order**
1. **Phase 1**: Basic merchant CRUD operations
2. **Phase 2**: Hierarchy tree view and navigation
3. **Phase 3**: Merchant settings management
4. **Phase 4**: User and role management
5. **Phase 5**: Advanced features (reports, analytics)

### 3. **Data Flow Examples**
```javascript
// Example: Creating a new retail
const createretail = async (retailData) => {
  // 1. Validate form data
  const errors = validateMerchantForm(retailData)
  if (Object.keys(errors).length > 0) {
    return { success: false, errors }
  }
  
  // 2. Call API
  const response = await fetch(`${API_BASE_URL}/merchants`, {
    method: 'POST',
    headers: apiHeaders,
    body: JSON.stringify({...retailData, merchant_type: 'retail'})
  })
  
  // 3. Handle response
  if (response.ok) {
    const newretail = await response.json()
    // Update local state
    addMerchantToStore(newretail)
    // Create default settings
    await createDefaultSettings(newretail.merchant_id)
    return { success: true, data: newretail }
  } else {
    const error = await response.json()
    return { success: false, error: error.detail }
  }
}
```

## πŸ”§ **Common Implementation Patterns**

### 1. **Autocomplete for Parent Selection**
```javascript
const ParentMerchantSelector = ({ merchantType, value, onChange }) => {
  const [options, setOptions] = useState([])
  
  useEffect(() => {
    const loadParentOptions = async () => {
      const parentType = getParentType(merchantType)
      const response = await fetch(`${API_BASE_URL}/merchants?merchant_type=${parentType}`)
      setOptions(await response.json())
    }
    
    if (merchantType) {
      loadParentOptions()
    }
  }, [merchantType])
  
  return (
    <Autocomplete
      options={options}
      getOptionLabel={(option) => `${option.merchant_name} (${option.merchant_code})`}
      value={value}
      onChange={onChange}
      placeholder="Select parent merchant..."
    />
  )
}
```

### 2. **Operating Hours Component**
```javascript
const OperatingHoursEditor = ({ value, onChange }) => {
  const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
  
  return (
    <div className="operating-hours-grid">
      {days.map(day => (
        <div key={day} className="day-row">
          <label>{day.charAt(0).toUpperCase() + day.slice(1)}</label>
          <input 
            type="checkbox"
            checked={value[day]?.is_open || false}
            onChange={(e) => onChange(day, 'is_open', e.target.checked)}
          />
          {value[day]?.is_open && (
            <>
              <input 
                type="time"
                value={value[day]?.open_time || '09:00'}
                onChange={(e) => onChange(day, 'open_time', e.target.value)}
              />
              <input 
                type="time"
                value={value[day]?.close_time || '18:00'} 
                onChange={(e) => onChange(day, 'close_time', e.target.value)}
              />
            </>
          )}
        </div>
      ))}
    </div>
  )
}
```

This prompt provides comprehensive guidance for building a production-ready UI that integrates seamlessly with your hierarchical merchant API system.