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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# Frontend Developer API Documentation
# Architecture PM System - KillaDesign

**Base URL**: `http://localhost:8000` (Development)  
**Framework**: FastAPI  
**Database**: PostgreSQL

---

## Table of Contents
1. [Dashboard](#dashboard)
2. [Projects](#projects)
3. [Employees](#employees)
4. [Timesheets](#timesheets)
5. [Milestones](#milestones)
6. [Invoices](#invoices)
7. [Subcontractors](#subcontractors)
8. [Staff Allocation](#staff-allocation)
9. [Data Models](#data-models)

---

## Dashboard

### Get Dashboard Statistics
Returns statistics for display on the home page.

**Endpoint**: `GET /`  
**Response Type**: HTML Page  
**Description**: Returns rendered HTML with dashboard statistics

**Response Context**:
```json
{
  "projects_count": 0,
  "employees_count": 0,
  "timesheets_count": 0,
  "invoices_count": 0
}
```

---

## Projects

### 1. List All Projects (HTML)
**Endpoint**: `GET /projects`  
**Response Type**: HTML  
**Description**: Returns rendered HTML page with projects list

---

### 2. List All Projects (API)
**Endpoint**: `GET /api/projects`  
**Response Type**: JSON  
**Description**: Returns array of all projects

**Response Example**:
```json
[
  {
    "project_id": "PROJ001",
    "project_name": "Villa Design",
    "client_name": "ABC Corporation",
    "contract_value_aed": 500000.00,
    "planned_cost_aed": 400000.00,
    "project_start_date": "2024-01-01",
    "project_end_date": "2024-12-31",
    "project_type": "Residential",
    "project_status": "Active",
    "current_phase": "Design Development",
    "created_at": "2024-01-01T10:00:00Z",
    "updated_at": "2024-01-01T10:00:00Z"
  }
]
```

---

### 3. Create Project
**Endpoint**: `POST /projects/create`  
**Content-Type**: `application/x-www-form-urlencoded`  
**Description**: Creates a new project

**Request Parameters**:
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| project_id | string | Yes | Unique project identifier |
| project_name | string | Yes | Name of the project |
| client_name | string | Yes | Name of the client |
| contract_value_aed | float | Yes | Contract value in AED |
| planned_cost_aed | float | Yes | Planned cost in AED |
| project_start_date | date | Yes | Start date (YYYY-MM-DD) |
| project_end_date | date | Yes | End date (YYYY-MM-DD) |
| project_type | string | Yes | Type of project |
| project_status | string | Yes | Current status |
| current_phase | string | No | Current project phase |

**Example Request (Form Data)**:
```
project_id=PROJ002
project_name=Modern Office Building
client_name=XYZ Ltd
contract_value_aed=1500000.00
planned_cost_aed=1200000.00
project_start_date=2024-02-01
project_end_date=2025-02-01
project_type=Commercial
project_status=Planning
current_phase=Initial Design
```

**Response**: Redirects to `/projects` (Status 303)

---

### 4. Delete Project
**Endpoint**: `POST /projects/delete/{project_id}`  
**Description**: Deletes a project by ID

**Path Parameters**:
- `project_id`: string - The project ID to delete

**Response**: Redirects to `/projects` (Status 303)

---

## Employees

### 1. List All Employees (HTML)
**Endpoint**: `GET /employees`  
**Response Type**: HTML  
**Description**: Returns rendered HTML page with employees list

---

### 2. List All Employees (API)
**Endpoint**: `GET /api/employees`  
**Response Type**: JSON  
**Description**: Returns array of all employees

**Response Example**:
```json
[
  {
    "employee_id": "EMP001",
    "employee_name": "John Doe",
    "department": "Architecture",
    "role": "Senior Architect",
    "hourly_rate_aed": 250.00,
    "employment_type": "Full-time",
    "start_date": "2023-01-15",
    "end_date": null,
    "cost_category": "Professional",
    "is_active": true,
    "email": "john.doe@example.com",
    "phone": "+971501234567",
    "created_at": "2023-01-15T09:00:00Z",
    "updated_at": "2023-01-15T09:00:00Z"
  }
]
```

---

### 3. Create Employee
**Endpoint**: `POST /employees/create`  
**Content-Type**: `application/x-www-form-urlencoded`  
**Description**: Creates a new employee

**Request Parameters**:
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| employee_id | string | Yes | Unique employee identifier |
| employee_name | string | Yes | Employee's full name |
| department | string | Yes | Department name |
| role | string | Yes | Job role/title |
| hourly_rate_aed | float | Yes | Hourly rate in AED |
| employment_type | string | Yes | Full-time/Part-time/Contract |
| start_date | date | Yes | Employment start date |
| cost_category | string | Yes | Cost category classification |
| email | string | No | Email address |
| phone | string | No | Phone number |

**Example Request (Form Data)**:
```
employee_id=EMP002
employee_name=Jane Smith
department=Interior Design
role=Interior Designer
hourly_rate_aed=180.00
employment_type=Full-time
start_date=2023-06-01
cost_category=Professional
email=jane.smith@example.com
phone=+971507654321
```

**Response**: Redirects to `/employees` (Status 303)

---

### 4. Delete Employee
**Endpoint**: `POST /employees/delete/{employee_id}`  
**Description**: Deletes an employee by ID

**Path Parameters**:
- `employee_id`: string - The employee ID to delete

**Response**: Redirects to `/employees` (Status 303)

---

## Timesheets

### 1. List All Timesheets (HTML)
**Endpoint**: `GET /timesheets`  
**Response Type**: HTML  
**Description**: Returns rendered HTML page with timesheets list, employees, and projects

---

### 2. List All Timesheets (API)
**Endpoint**: `GET /api/timesheets`  
**Response Type**: JSON  
**Description**: Returns array of timesheets (limited to 200 records)

**Response Example**:
```json
[
  {
    "record_id": "TS001",
    "date": "2024-01-15",
    "employee_id": "EMP001",
    "project_id": "PROJ001",
    "hours_worked": 8.00,
    "billable_hours": 7.50,
    "work_category": "Design",
    "task_description": "Conceptual drawings and sketches",
    "is_approved": false,
    "created_at": "2024-01-15T18:00:00Z",
    "updated_at": "2024-01-15T18:00:00Z"
  }
]
```

---

### 3. Create Timesheet
**Endpoint**: `POST /timesheets/create`  
**Content-Type**: `application/x-www-form-urlencoded`  
**Description**: Creates a new timesheet entry

**Request Parameters**:
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| record_id | string | Yes | Unique record identifier |
| date | date | Yes | Date of work (YYYY-MM-DD) |
| employee_id | string | Yes | Employee ID (FK) |
| project_id | string | Yes | Project ID (FK) |
| hours_worked | float | Yes | Total hours worked |
| billable_hours | float | Yes | Billable hours |
| work_category | string | Yes | Category of work |
| task_description | string | No | Description of tasks |

**Example Request (Form Data)**:
```
record_id=TS002
date=2024-01-16
employee_id=EMP001
project_id=PROJ001
hours_worked=8.00
billable_hours=8.00
work_category=Design Development
task_description=Detailed floor plans and elevations
```

**Response**: Redirects to `/timesheets` (Status 303)

---

### 4. Delete Timesheet
**Endpoint**: `POST /timesheets/delete/{record_id}`  
**Description**: Deletes a timesheet entry by ID

**Path Parameters**:
- `record_id`: string - The timesheet record ID to delete

**Response**: Redirects to `/timesheets` (Status 303)

---

## Milestones

### 1. List All Milestones (HTML)
**Endpoint**: `GET /milestones`  
**Response Type**: HTML  
**Description**: Returns rendered HTML page with milestones list and projects

---

### 2. List All Milestones (API)
**Endpoint**: `GET /api/milestones`  
**Response Type**: JSON  
**Description**: Returns array of all milestones

**Response Example**:
```json
[
  {
    "milestone_id": 1,
    "project_id": "PROJ001",
    "milestone_name": "Concept Design Approval",
    "milestone_order": 1,
    "planned_date": "2024-02-15",
    "actual_date": "2024-02-14",
    "status": "Completed",
    "completion_percentage": 100,
    "created_at": "2024-01-01T10:00:00Z",
    "updated_at": "2024-02-14T16:00:00Z"
  }
]
```

---

### 3. Create Milestone
**Endpoint**: `POST /milestones/create`  
**Content-Type**: `application/x-www-form-urlencoded`  
**Description**: Creates a new milestone

**Request Parameters**:
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| project_id | string | Yes | Project ID (FK) |
| milestone_name | string | Yes | Name of milestone |
| milestone_order | integer | Yes | Order/sequence number |
| planned_date | date | Yes | Planned completion date |
| status | string | Yes | Current status |
| completion_percentage | integer | No | Completion % (0-100) |
| actual_date | date | No | Actual completion date |

**Example Request (Form Data)**:
```
project_id=PROJ001
milestone_name=Design Development Complete
milestone_order=2
planned_date=2024-04-30
status=In Progress
completion_percentage=45
```

**Response**: Redirects to `/milestones` (Status 303)

---

### 4. Delete Milestone
**Endpoint**: `POST /milestones/delete/{milestone_id}`  
**Description**: Deletes a milestone by ID

**Path Parameters**:
- `milestone_id`: integer - The milestone ID to delete

**Response**: Redirects to `/milestones` (Status 303)

---

## Invoices

### 1. List All Invoices (HTML)
**Endpoint**: `GET /invoices`  
**Response Type**: HTML  
**Description**: Returns rendered HTML page with invoices list and projects

---

### 2. List All Invoices (API)
**Endpoint**: `GET /api/invoices`  
**Response Type**: JSON  
**Description**: Returns array of all invoices

**Response Example**:
```json
[
  {
    "invoice_id": "INV001",
    "project_id": "PROJ001",
    "invoice_date": "2024-02-01",
    "invoice_amount_aed": 50000.00,
    "due_date": "2024-03-01",
    "payment_date": "2024-02-28",
    "payment_status": "Paid",
    "days_outstanding": null,
    "milestone_reference": "Concept Design Approval",
    "paid_amount_aed": 50000.00,
    "created_at": "2024-02-01T10:00:00Z",
    "updated_at": "2024-02-28T14:30:00Z"
  }
]
```

---

### 3. Create Invoice
**Endpoint**: `POST /invoices/create`  
**Content-Type**: `application/x-www-form-urlencoded`  
**Description**: Creates a new invoice

**Request Parameters**:
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| invoice_id | string | Yes | Unique invoice identifier |
| project_id | string | Yes | Project ID (FK) |
| invoice_date | date | Yes | Invoice issue date |
| invoice_amount_aed | float | Yes | Invoice amount in AED |
| due_date | date | Yes | Payment due date |
| payment_status | string | Yes | Payment status |
| payment_date | date | No | Actual payment date |
| milestone_reference | string | No | Related milestone |

**Example Request (Form Data)**:
```
invoice_id=INV002
project_id=PROJ001
invoice_date=2024-05-01
invoice_amount_aed=75000.00
due_date=2024-05-31
payment_status=Pending
milestone_reference=Design Development Complete
```

**Response**: Redirects to `/invoices` (Status 303)

---

### 4. Delete Invoice
**Endpoint**: `POST /invoices/delete/{invoice_id}`  
**Description**: Deletes an invoice by ID

**Path Parameters**:
- `invoice_id`: string - The invoice ID to delete

**Response**: Redirects to `/invoices` (Status 303)

---

## Subcontractors

### 1. List All Subcontractors (HTML)
**Endpoint**: `GET /subcontractors`  
**Response Type**: HTML  
**Description**: Returns rendered HTML page with subcontractors list and projects

---

### 2. List All Subcontractors (API)
**Endpoint**: `GET /api/subcontractors`  
**Response Type**: JSON  
**Description**: Returns array of all subcontractors

**Response Example**:
```json
[
  {
    "subcontractor_id": 1,
    "project_id": "PROJ001",
    "subcontractor_name": "Elite MEP Services",
    "service_type": "MEP Consultation",
    "contract_amount_aed": 150000.00,
    "amount_invoiced_aed": 50000.00,
    "payment_status": "Partially Paid",
    "work_category": "MEP Design",
    "contract_start_date": "2024-02-01",
    "contract_end_date": "2024-12-31",
    "contact_person": "Ahmed Ali",
    "contact_email": "ahmed@elitemep.com",
    "contact_phone": "+971504567890",
    "created_at": "2024-02-01T09:00:00Z",
    "updated_at": "2024-02-01T09:00:00Z"
  }
]
```

---

### 3. Create Subcontractor
**Endpoint**: `POST /subcontractors/create`  
**Content-Type**: `application/x-www-form-urlencoded`  
**Description**: Creates a new subcontractor

**Request Parameters**:
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| project_id | string | Yes | Project ID (FK) |
| subcontractor_name | string | Yes | Subcontractor company name |
| service_type | string | Yes | Type of service provided |
| contract_amount_aed | float | Yes | Contract amount in AED |
| amount_invoiced_aed | float | No | Amount invoiced (default: 0) |
| payment_status | string | Yes | Payment status |
| work_category | string | Yes | Work category |
| contact_person | string | No | Contact person name |
| contact_email | string | No | Contact email |

**Example Request (Form Data)**:
```
project_id=PROJ001
subcontractor_name=Structural Engineering Co
service_type=Structural Design
contract_amount_aed=80000.00
amount_invoiced_aed=0
payment_status=Not Started
work_category=Structural
contact_person=Mohammed Hassan
contact_email=mohammed@structural.com
```

**Response**: Redirects to `/subcontractors` (Status 303)

---

### 4. Delete Subcontractor
**Endpoint**: `POST /subcontractors/delete/{subcontractor_id}`  
**Description**: Deletes a subcontractor by ID

**Path Parameters**:
- `subcontractor_id`: integer - The subcontractor ID to delete

**Response**: Redirects to `/subcontractors` (Status 303)

---

## Staff Allocation

### 1. List All Staff Allocations (HTML)
**Endpoint**: `GET /staff-allocation`  
**Response Type**: HTML  
**Description**: Returns rendered HTML page with staff allocations, projects, employees, and milestones

---

### 2. List All Staff Allocations (API)
**Endpoint**: `GET /api/staff-allocation`  
**Response Type**: JSON  
**Description**: Returns array of all staff allocations

**Response Example**:
```json
[
  {
    "allocation_id": 1,
    "project_id": "PROJ001",
    "milestone_id": 1,
    "milestone_name": "Concept Design Approval",
    "employee_id": "EMP001",
    "employee_name": "John Doe",
    "role": "Senior Architect",
    "category": "Professional",
    "hours_allocated": 160.00,
    "hours_worked": 142.50,
    "hourly_rate_aed": 250.00,
    "skill_match_score": 95,
    "availability_status": "Available",
    "performance_rating": "Excellent",
    "start_date": "2024-01-01",
    "end_date": "2024-02-15",
    "notes": "Lead designer for concept phase",
    "created_at": "2024-01-01T10:00:00Z",
    "updated_at": "2024-02-15T16:00:00Z"
  }
]
```

---

### 3. Create Staff Allocation
**Endpoint**: `POST /staff-allocation/create`  
**Content-Type**: `application/x-www-form-urlencoded`  
**Description**: Creates a new staff allocation

**Request Parameters**:
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| project_id | string | Yes | Project ID (FK) |
| employee_id | string | Yes | Employee ID (FK) |
| employee_name | string | Yes | Employee's full name |
| role | string | Yes | Role/position |
| hours_allocated | float | Yes | Hours allocated |
| hours_worked | float | No | Hours worked (default: 0) |
| hourly_rate_aed | float | Yes | Hourly rate in AED |
| start_date | date | Yes | Allocation start date |
| milestone_id | integer | No | Milestone ID (FK) |
| milestone_name | string | No | Milestone name |
| category | string | No | Category classification |
| skill_match_score | integer | No | Skill match score (0-100) |
| availability_status | string | No | Availability status |
| performance_rating | string | No | Performance rating |
| end_date | date | No | Allocation end date |
| notes | string | No | Additional notes |

**Example Request (Form Data)**:
```
project_id=PROJ001
employee_id=EMP001
employee_name=John Doe
role=Senior Architect
hours_allocated=160.00
hours_worked=0
hourly_rate_aed=250.00
start_date=2024-03-01
milestone_id=2
milestone_name=Design Development Complete
category=Professional
skill_match_score=95
availability_status=Available
performance_rating=Excellent
end_date=2024-04-30
notes=Lead architect for design development phase
```

**Response**: Redirects to `/staff-allocation` (Status 303)

---

### 4. Delete Staff Allocation
**Endpoint**: `POST /staff-allocation/delete/{allocation_id}`  
**Description**: Deletes a staff allocation by ID

**Path Parameters**:
- `allocation_id`: integer - The allocation ID to delete

**Response**: Redirects to `/staff-allocation` (Status 303)

---

## Data Models

### Project Model
```typescript
interface Project {
  project_id: string;          // Primary Key
  project_name: string;
  client_name: string;
  contract_value_aed: number;  // Decimal(15,2)
  planned_cost_aed: number;    // Decimal(15,2)
  project_start_date: string;  // Date (YYYY-MM-DD)
  project_end_date: string;    // Date (YYYY-MM-DD)
  project_type: string;
  project_status: string;
  current_phase?: string;
  created_at?: string;         // DateTime
  updated_at?: string;         // DateTime
}
```

### Employee Model
```typescript
interface Employee {
  employee_id: string;         // Primary Key
  employee_name: string;
  department: string;
  role: string;
  hourly_rate_aed: number;     // Decimal(10,2)
  employment_type: string;
  start_date: string;          // Date
  end_date?: string;           // Date
  cost_category: string;
  is_active: boolean;
  email?: string;
  phone?: string;
  created_at?: string;         // DateTime
  updated_at?: string;         // DateTime
}
```

### Timesheet Model
```typescript
interface Timesheet {
  record_id: string;           // Primary Key
  date: string;                // Date
  employee_id: string;         // Foreign Key
  project_id: string;          // Foreign Key
  hours_worked: number;        // Decimal(5,2)
  billable_hours: number;      // Decimal(5,2)
  work_category: string;
  task_description?: string;
  is_approved: boolean;
  approved_by?: string;
  approved_at?: string;        // DateTime
  created_at?: string;         // DateTime
  updated_at?: string;         // DateTime
}
```

### Milestone Model
```typescript
interface Milestone {
  milestone_id: number;        // Primary Key (Auto)
  project_id: string;          // Foreign Key
  milestone_name: string;
  milestone_order: number;     // Integer
  planned_date: string;        // Date
  actual_date?: string;        // Date
  status: string;
  completion_percentage: number; // Integer (0-100)
  created_at?: string;         // DateTime
  updated_at?: string;         // DateTime
}
```

### Invoice Model
```typescript
interface Invoice {
  invoice_id: string;          // Primary Key
  project_id: string;          // Foreign Key
  invoice_date: string;        // Date
  invoice_amount_aed: number;  // Decimal(15,2)
  due_date: string;            // Date
  payment_date?: string;       // Date
  payment_status: string;
  days_outstanding?: number;   // Integer
  milestone_reference?: string;
  paid_amount_aed: number;     // Decimal(15,2)
  created_at?: string;         // DateTime
  updated_at?: string;         // DateTime
}
```

### Subcontractor Model
```typescript
interface Subcontractor {
  subcontractor_id: number;    // Primary Key (Auto)
  project_id: string;          // Foreign Key
  subcontractor_name: string;
  service_type: string;
  contract_amount_aed: number; // Decimal(15,2)
  amount_invoiced_aed: number; // Decimal(15,2)
  payment_status: string;
  work_category: string;
  contract_start_date?: string; // Date
  contract_end_date?: string;   // Date
  contact_person?: string;
  contact_email?: string;
  contact_phone?: string;
  created_at?: string;         // DateTime
  updated_at?: string;         // DateTime
}
```

### Staff Allocation Model
```typescript
interface StaffAllocation {
  allocation_id: number;       // Primary Key (Auto)
  project_id: string;          // Foreign Key
  milestone_id?: number;       // Foreign Key
  milestone_name?: string;
  employee_id: string;         // Foreign Key
  employee_name: string;
  role: string;
  category?: string;
  hours_allocated: number;     // Decimal(10,2)
  hours_worked: number;        // Decimal(10,2)
  hourly_rate_aed: number;     // Decimal(10,2)
  skill_match_score?: number;  // Integer
  availability_status?: string;
  performance_rating?: string;
  start_date: string;          // Date
  end_date?: string;           // Date
  notes?: string;              // Text
  created_at?: string;         // DateTime
  updated_at?: string;         // DateTime
}
```

---

## Common Response Formats

### Success Response (JSON Endpoints)
```json
{
  "data": [ /* array of objects */ ]
}
```

### Error Response
```json
{
  "detail": "Error message description"
}
```

### HTTP Status Codes
- `200 OK` - Successful GET request
- `303 See Other` - Successful POST request (redirects)
- `404 Not Found` - Resource not found
- `422 Unprocessable Entity` - Validation error
- `500 Internal Server Error` - Server error

---

## Frontend Implementation Notes

### 1. **API Calls**
- All HTML endpoints return rendered pages
- All `/api/*` endpoints return JSON data
- Use `/api/*` endpoints for AJAX/Fetch requests
- POST endpoints use form-urlencoded data and return redirects

### 2. **Date Format**
- Input: `YYYY-MM-DD` (e.g., "2024-01-15")
- Output: ISO format with timezone

### 3. **Decimal Values**
- All monetary values are in AED
- Use 2 decimal places for currency
- Hours can use up to 2 decimal places

### 4. **Foreign Key Relationships**
- **Timesheets**: Requires valid `employee_id` and `project_id`
- **Milestones**: Requires valid `project_id`
- **Invoices**: Requires valid `project_id`
- **Subcontractors**: Requires valid `project_id`
- **Staff Allocation**: Requires valid `project_id`, `employee_id`, optional `milestone_id`

### 5. **Cascade Deletes**
- Deleting a **Project** will also delete:
  - All Milestones
  - All Subcontractors
  - All Staff Allocations
- Deleting a **Milestone** will set `milestone_id` to NULL in Staff Allocations

### 6. **Form Submission**
All POST endpoints expect `application/x-www-form-urlencoded` data:

```javascript
// Example using Fetch API
const formData = new FormData();
formData.append('project_id', 'PROJ001');
formData.append('project_name', 'New Project');
// ... add more fields

fetch('/projects/create', {
  method: 'POST',
  body: new URLSearchParams(formData)
});
```

### 7. **AJAX Data Fetching**
For dynamic data loading, use the `/api/*` endpoints:

```javascript
// Example: Fetch all projects
fetch('/api/projects')
  .then(response => response.json())
  .then(data => {
    // data is an array of project objects
    console.log(data);
  });
```

---

## Database Connection
- **Type**: PostgreSQL
- **Hosted on**: Render.com
- **Connection**: Managed by SQLAlchemy ORM
- **Session Management**: Automatic via dependency injection

---

## CRUD Operations Summary

| Resource | List (JSON) | Create | Update | Delete | Get Single |
|----------|------------|--------|--------|--------|------------|
| Projects | βœ… `/api/projects` | βœ… | βœ… (in CRUD) | βœ… | βœ… (in CRUD) |
| Employees | βœ… `/api/employees` | βœ… | βœ… (in CRUD) | βœ… | βœ… (in CRUD) |
| Timesheets | βœ… `/api/timesheets` | βœ… | βœ… (in CRUD) | βœ… | βœ… (in CRUD) |
| Milestones | βœ… `/api/milestones` | βœ… | βœ… (in CRUD) | βœ… | βœ… (in CRUD) |
| Invoices | βœ… `/api/invoices` | βœ… | βœ… (in CRUD) | βœ… | βœ… (in CRUD) |
| Subcontractors | βœ… `/api/subcontractors` | βœ… | βœ… (in CRUD) | βœ… | βœ… (in CRUD) |
| Staff Allocation | βœ… `/api/staff-allocation` | ⚠️ (in CRUD only) | ⚠️ (in CRUD only) | ⚠️ (in CRUD only) | ⚠️ (in CRUD only) |

**Note**: Operations marked with ⚠️ are implemented in `crud.py` but not exposed as API endpoints in `main.py`. They can be added if needed.

---

## Additional Features Available in CRUD Layer

The following functions are available in `crud.py` but not exposed as API endpoints:

### Get Single Record
- `get_project(db, project_id: str)`
- `get_employee(db, employee_id: str)`
- `get_timesheet(db, record_id: str)`
- `get_milestone(db, milestone_id: int)`
- `get_invoice(db, invoice_id: str)`
- `get_subcontractor(db, subcontractor_id: int)`
- `get_staff_allocation(db, allocation_id: int)`

### Update Record
- `update_project(db, project_id: str, project: ProjectCreate)`
- `update_employee(db, employee_id: str, employee: EmployeeCreate)`
- `update_timesheet(db, record_id: str, timesheet: TimesheetCreate)`
- `update_milestone(db, milestone_id: int, milestone: MilestoneCreate)`
- `update_invoice(db, invoice_id: str, invoice: InvoiceCreate)`
- `update_subcontractor(db, subcontractor_id: int, subcontractor: SubcontractorCreate)`
- `update_staff_allocation(db, allocation_id: int, allocation: StaffAllocationCreate)`

**If you need these operations exposed as API endpoints, they can be added to `main.py`.**

---

## Contact & Support
For backend modifications or additional endpoints, contact the backend development team.

---

**Document Version**: 1.0  
**Last Updated**: 2025-10-08  
**Generated for**: Frontend Development Team