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
 
 
 
 
 
 
 
cce9e60
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
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
922
923
924
925
926
927
928
929
930
931
932
933
934
from fastapi import APIRouter, Depends, Query, status, HTTPException, Response
from sqlalchemy.orm import Session
from app.db.session import get_db
from app.services.reference_service import ReferenceDataService
from app.schemas.reference import (
    StateOut, CountryOut, CompanyTypeOut, LeadSourceOut, PaymentTermOut,
    PurchasePriceOut, RentalPriceOut, BarrierSizeOut, ProductApplicationOut,
    ReferenceDataResponse, FOBOut, EstShipDateOut, EstFreightOut, StatusInfoOut, ProjectReferenceResponse, PriorityOut,
    StateCreateIn, StateUpdateIn, CountryCreateIn, CountryUpdateIn, CompanyTypeCreateIn, CompanyTypeUpdateIn,
    LeadSourceCreateIn, LeadSourceUpdateIn, PaymentTermCreateIn, PaymentTermUpdateIn,
    PurchasePriceCreateIn, PurchasePriceUpdateIn, RentalPriceCreateIn, RentalPriceUpdateIn,
    BarrierSizeCreateIn, BarrierSizeUpdateIn, ProductApplicationCreateIn, ProductApplicationUpdateIn,
    FOBCreateIn, FOBUpdateIn, EstShipDateCreateIn, EstShipDateUpdateIn,
    EstFreightCreateIn, EstFreightUpdateIn, StatusInfoCreateIn, StatusInfoUpdateIn,
    PriorityCreateIn, PriorityUpdateIn, PhoneTypeOut, PhoneTypeCreateIn, PhoneTypeUpdateIn
)
from app.core.dependencies import get_current_user_optional
from app.schemas.auth import CurrentUser
from typing import List, Optional
import logging

logger = logging.getLogger(__name__)

router = APIRouter(prefix="/api/v1/reference", tags=["reference-data"])

@router.get("/all", response_model=ReferenceDataResponse)
def get_all_reference_data(
    active_only: bool = Query(True, description="Return only active records"),
    customer_type_id: Optional[int] = Query(None, description="Filter by customer type ID"),
    db: Session = Depends(get_db),
    current_user: Optional[CurrentUser] = Depends(get_current_user_optional)
):
    """
    Get all reference/lookup data in a single response.
    
    This endpoint is designed for frontend applications that need to populate
    all dropdowns and lookup data efficiently. It includes:
    - States/Provinces
    - Countries
    - Company Types
    - Lead Sources
    - Payment Terms
    - Purchase Prices
    - Rental Prices
    - Barrier Sizes
    - Product Applications
    - Project Statuses
    
    **Note**: This endpoint is public but may have enhanced data for authenticated users.
    """
    service = ReferenceDataService(db)
    return service.get_all_reference_data(active_only=active_only, customer_type_id=customer_type_id)

@router.get("/states", response_model=List[StateOut])
def get_states(
    active_only: bool = Query(True, description="Return only active states"),
    customer_type_id: Optional[int] = Query(None, description="Filter by customer type ID"),
    db: Session = Depends(get_db)
):
    """
    Get all states/provinces.
    
    Returns a list of all states and provinces available in the system.
    """
    service = ReferenceDataService(db)
    return service.get_states(active_only=active_only, customer_type_id=customer_type_id)

@router.get("/countries", response_model=List[CountryOut])
def get_countries(
    active_only: bool = Query(True, description="Return only active countries"),
    db: Session = Depends(get_db)
):
    """
    Get all countries.
    
    Returns a list of all countries available in the system.
    """
    service = ReferenceDataService(db)
    return service.get_countries(active_only=active_only)

@router.get("/company-types", response_model=List[CompanyTypeOut])
def get_company_types(
    active_only: bool = Query(True, description="Return only active company types"),
    customer_type_id: Optional[int] = Query(None, description="Filter by customer type ID"),
    db: Session = Depends(get_db)
):
    """
    Get all company types.
    
    Returns a list of all company types used for customer classification.
    Optionally filtered by customer_type_id.
    """
    service = ReferenceDataService(db)
    return service.get_company_types(active_only=active_only, customer_type_id=customer_type_id)

@router.get("/lead-sources", response_model=List[LeadSourceOut])
def get_lead_sources(
    active_only: bool = Query(True, description="Return only active lead sources"),
    customer_type_id: Optional[int] = Query(None, description="Filter by customer type ID"),
    db: Session = Depends(get_db)
):
    """
    Get all lead generation sources.
    
    Returns a list of all sources where leads can be generated from.
    Optionally filtered by customer_type_id.
    """
    service = ReferenceDataService(db)
    return service.get_lead_sources(active_only=active_only, customer_type_id=customer_type_id)

@router.get("/payment-terms", response_model=List[PaymentTermOut])
def get_payment_terms(
    active_only: bool = Query(True, description="Return only active payment terms"),
    db: Session = Depends(get_db)
):
    """
    Get all payment terms.
    
    Returns a list of all available payment terms for transactions.
    """
    service = ReferenceDataService(db)
    return service.get_payment_terms(active_only=active_only)

@router.get("/purchase-prices", response_model=List[PurchasePriceOut])
def get_purchase_prices(
    active_only: bool = Query(True, description="Return only active purchase prices"),
    db: Session = Depends(get_db)
):
    """
    Get all purchase prices.
    
    Returns a list of all available purchase price options.
    """
    service = ReferenceDataService(db)
    return service.get_purchase_prices(active_only=active_only)

@router.get("/rental-prices", response_model=List[RentalPriceOut])
def get_rental_prices(
    active_only: bool = Query(True, description="Return only active rental prices"),
    db: Session = Depends(get_db)
):
    """
    Get all rental prices.
    
    Returns a list of all available rental price options.
    """
    service = ReferenceDataService(db)
    return service.get_rental_prices(active_only=active_only)



@router.get("/product-applications", response_model=List[ProductApplicationOut])
def get_product_applications(
    active_only: bool = Query(True, description="Return only active product applications"),
    db: Session = Depends(get_db)
):
    """
    Get all product applications.
    
    Returns a list of all available product application types.
    """
    service = ReferenceDataService(db)
    return service.get_product_applications(active_only=active_only)

@router.get("/fobs", response_model=List[FOBOut])
def get_FOB(
    active_only: bool = Query(True, description="Return only active FOBS"),
    db: Session = Depends(get_db)
):
    """
    Get all FOBs (Free on Board terms).
    
    Returns a list of all available FOB terms used in shipping.
    """
    service = ReferenceDataService(db)
    return service.get_fobs(active_only=active_only)


@router.get("/est-ship-dates", response_model=List[EstShipDateOut])
def get_est_ship_dates(
    active_only: bool = Query(True, description="Return only active estimated ship dates"),
    db: Session = Depends(get_db)
):
    """
    Get all Estimated Ship Date options.
    Returns a list of estimated shipping date options from the DB.
    """
    service = ReferenceDataService(db)
    return service.get_est_ship_dates(active_only=active_only)


@router.get("/est-freights", response_model=List[EstFreightOut])
def get_est_freights(
    active_only: bool = Query(True, description="Return only active estimated freights"),
    db: Session = Depends(get_db)
):
    """
    Get all Estimated Freight options.
    Returns a list of estimated freight options from the DB.
    """
    service = ReferenceDataService(db)
    return service.get_est_freights(active_only=active_only)


@router.get("/status-info", response_model=List[StatusInfoOut])
def get_status_info(
    active_only: bool = Query(True, description="Return only active status info records"),
    db: Session = Depends(get_db)
):
    """
    Get all StatusInfo records.
    Returns a list of project status metadata (ID, description, abbreviation).
    """
    service = ReferenceDataService(db)
    return service.get_status_info(active_only=active_only)


@router.get("/project-reference", response_model=ProjectReferenceResponse)
def get_project_reference(
    active_only: bool = Query(True, description="Return only active project reference records"),
    db: Session = Depends(get_db)
):
    """
    Return grouped reference data used for project creation forms.
    Only returns the subset of reference tables needed by project forms to reduce payload.
    """
    service = ReferenceDataService(db)
    return service.get_project_reference(active_only=active_only)

@router.post("/clear-cache", status_code=status.HTTP_200_OK)
def clear_reference_cache(
    current_user: CurrentUser = Depends(get_current_user_optional),
    db: Session = Depends(get_db)
):
    """
    Clear the reference data cache.
    
    Forces fresh data to be loaded on the next request.
    This endpoint may require authentication in production.
    """
    service = ReferenceDataService(db)
    service.clear_cache()
    return {"message": "Reference data cache cleared successfully"}

@router.get("/priorities", response_model=List[PriorityOut])
def get_priorities(
    active_only: bool = Query(True, description="Return only active priorities"),
    customer_type_id: Optional[int] = Query(None, description="Filter by customer type ID"),
    db: Session = Depends(get_db)
):
    """
    Get all priorities.
    
    Returns a list of all available priorities, optionally filtered by customer type.
    """
    service = ReferenceDataService(db)
    return service.get_priorities(active_only=active_only, customer_type_id=customer_type_id)

@router.post("/states", response_model=StateOut, status_code=status.HTTP_201_CREATED)
def create_state(
    data: StateCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new state/province.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_state(data)

@router.put("/states/{state_id}", response_model=StateOut)
def update_state(
    state_id: int,
    data: StateUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing state/province.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_state(state_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="State not found")
    return result

@router.post("/countries", response_model=CountryOut, status_code=status.HTTP_201_CREATED)
def create_country(
    data: CountryCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new country.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_country(data)

@router.put("/countries/{country_id}", response_model=CountryOut)
def update_country(
    country_id: int,
    data: CountryUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing country.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_country(country_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Country not found")
    return result

@router.post("/company-types", response_model=CompanyTypeOut, status_code=status.HTTP_201_CREATED)
def create_company_type(
    data: CompanyTypeCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new company type.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_company_type(data)

@router.put("/company-types/{company_type_id}", response_model=CompanyTypeOut)
def update_company_type(
    company_type_id: int,
    data: CompanyTypeUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing company type.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_company_type(company_type_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Company type not found")
    return result

@router.post("/lead-sources", response_model=LeadSourceOut, status_code=status.HTTP_201_CREATED)
def create_lead_source(
    data: LeadSourceCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new lead source.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_lead_source(data)

@router.put("/lead-sources/{lead_generated_from_id}", response_model=LeadSourceOut)
def update_lead_source(
    lead_generated_from_id: int,
    data: LeadSourceUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing lead source.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_lead_source(lead_generated_from_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Lead source not found")
    return result

@router.post("/payment-terms", response_model=PaymentTermOut, status_code=status.HTTP_201_CREATED)
def create_payment_term(
    data: PaymentTermCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new payment term.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_payment_term(data)

@router.put("/payment-terms/{payment_term_id}", response_model=PaymentTermOut)
def update_payment_term(
    payment_term_id: int,
    data: PaymentTermUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing payment term.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_payment_term(payment_term_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Payment term not found")
    return result

@router.post("/purchase-prices", response_model=PurchasePriceOut, status_code=status.HTTP_201_CREATED)
def create_purchase_price(
    data: PurchasePriceCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new purchase price.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_purchase_price(data)

@router.put("/purchase-prices/{purchase_price_id}", response_model=PurchasePriceOut)
def update_purchase_price(
    purchase_price_id: int,
    data: PurchasePriceUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing purchase price.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_purchase_price(purchase_price_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Purchase price not found")
    return result

@router.post("/rental-prices", response_model=RentalPriceOut, status_code=status.HTTP_201_CREATED)
def create_rental_price(
    data: RentalPriceCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new rental price.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_rental_price(data)

@router.put("/rental-prices/{rental_price_id}", response_model=RentalPriceOut)
def update_rental_price(
    rental_price_id: int,
    data: RentalPriceUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing rental price.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_rental_price(rental_price_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Rental price not found")
    return result

@router.post("/barrier-sizes", response_model=BarrierSizeOut, status_code=status.HTTP_201_CREATED)
def create_barrier_size(
    data: BarrierSizeCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new barrier size.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_barrier_size(data)

@router.put("/barrier-sizes/{barrier_size_id}", response_model=BarrierSizeOut)
def update_barrier_size(
    barrier_size_id: int,
    data: BarrierSizeUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing barrier size.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_barrier_size(barrier_size_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Barrier size not found")
    return result

@router.post("/product-applications", response_model=ProductApplicationOut, status_code=status.HTTP_201_CREATED)
def create_product_application(
    data: ProductApplicationCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new product application.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_product_application(data)

@router.put("/product-applications/{application_id}", response_model=ProductApplicationOut)
def update_product_application(
    application_id: int,
    data: ProductApplicationUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing product application.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_product_application(application_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Product application not found")
    return result

@router.post("/fobs", response_model=FOBOut, status_code=status.HTTP_201_CREATED)
def create_fob(
    data: FOBCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new FOB term.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_fob(data)

@router.put("/fobs/{fob_id}", response_model=FOBOut)
def update_fob(
    fob_id: int,
    data: FOBUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing FOB term.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_fob(fob_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="FOB not found")
    return result

@router.post("/est-ship-dates", response_model=EstShipDateOut, status_code=status.HTTP_201_CREATED)
def create_est_ship_date(
    data: EstShipDateCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new estimated ship date option.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_est_ship_date(data)

@router.put("/est-ship-dates/{est_ship_date_id}", response_model=EstShipDateOut)
def update_est_ship_date(
    est_ship_date_id: int,
    data: EstShipDateUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing estimated ship date option.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_est_ship_date(est_ship_date_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Estimated ship date not found")
    return result

@router.post("/est-freights", response_model=EstFreightOut, status_code=status.HTTP_201_CREATED)
def create_est_freight(
    data: EstFreightCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new estimated freight option.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_est_freight(data)

@router.put("/est-freights/{est_freight_id}", response_model=EstFreightOut)
def update_est_freight(
    est_freight_id: int,
    data: EstFreightUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing estimated freight option.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_est_freight(est_freight_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Estimated freight not found")
    return result

@router.post("/status-info", response_model=StatusInfoOut, status_code=status.HTTP_201_CREATED)
def create_status_info(
    data: StatusInfoCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new status info record.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_status_info(data)

@router.put("/status-info/{status_info_id}", response_model=StatusInfoOut)
def update_status_info(
    status_info_id: int,
    data: StatusInfoUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing status info record.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_status_info(status_info_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Status info not found")
    return result

@router.post("/priorities", response_model=PriorityOut, status_code=status.HTTP_201_CREATED)
def create_priority(
    data: PriorityCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Create a new priority.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    return service.create_priority(data)

@router.put("/priorities/{priority_id}", response_model=PriorityOut)
def update_priority(
    priority_id: int,
    data: PriorityUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """
    Update an existing priority.
    
    Requires authentication for production use.
    """
    service = ReferenceDataService(db)
    result = service.update_priority(priority_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Priority not found")
    return result

@router.delete("/states/{state_id}", status_code=204)
def delete_state(
    state_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a state"""
    service = ReferenceDataService(db)
    success = service.delete_state(state_id)
    if not success:
        raise HTTPException(status_code=404, detail="State not found")
    return Response(status_code=204)

@router.delete("/countries/{country_id}", status_code=204)
def delete_country(
    country_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a country"""
    service = ReferenceDataService(db)
    success = service.delete_country(country_id)
    if not success:
        raise HTTPException(status_code=404, detail="Country not found")
    return Response(status_code=204)

@router.delete("/company-types/{company_type_id}", status_code=204)
def delete_company_type(
    company_type_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a company type"""
    service = ReferenceDataService(db)
    success = service.delete_company_type(company_type_id)
    if not success:
        raise HTTPException(status_code=404, detail="Company type not found")
    return Response(status_code=204)

@router.delete("/lead-sources/{lead_generated_from_id}", status_code=204)
def delete_lead_source(
    lead_generated_from_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a lead source"""
    service = ReferenceDataService(db)
    success = service.delete_lead_source(lead_generated_from_id)
    if not success:
        raise HTTPException(status_code=404, detail="Lead source not found")
    return Response(status_code=204)

@router.delete("/payment-terms/{payment_term_id}", status_code=204)
def delete_payment_term(
    payment_term_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a payment term"""
    service = ReferenceDataService(db)
    success = service.delete_payment_term(payment_term_id)
    if not success:
        raise HTTPException(status_code=404, detail="Payment term not found")
    return Response(status_code=204)

@router.delete("/purchase-prices/{purchase_price_id}", status_code=204)
def delete_purchase_price(
    purchase_price_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a purchase price"""
    service = ReferenceDataService(db)
    success = service.delete_purchase_price(purchase_price_id)
    if not success:
        raise HTTPException(status_code=404, detail="Purchase price not found")
    return Response(status_code=204)

@router.delete("/rental-prices/{rental_price_id}", status_code=204)
def delete_rental_price(
    rental_price_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a rental price"""
    service = ReferenceDataService(db)
    success = service.delete_rental_price(rental_price_id)
    if not success:
        raise HTTPException(status_code=404, detail="Rental price not found")
    return Response(status_code=204)

@router.delete("/barrier-sizes/{barrier_size_id}", status_code=204)
def delete_barrier_size(
    barrier_size_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a barrier size"""
    service = ReferenceDataService(db)
    success = service.delete_barrier_size(barrier_size_id)
    if not success:
        raise HTTPException(status_code=404, detail="Barrier size not found")
    return Response(status_code=204)

@router.delete("/product-applications/{application_id}", status_code=204)
def delete_product_application(
    application_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a product application"""
    service = ReferenceDataService(db)
    success = service.delete_product_application(application_id)
    if not success:
        raise HTTPException(status_code=404, detail="Product application not found")
    return Response(status_code=204)

@router.delete("/fobs/{fob_id}", status_code=204)
def delete_fob(
    fob_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a FOB"""
    service = ReferenceDataService(db)
    success = service.delete_fob(fob_id)
    if not success:
        raise HTTPException(status_code=404, detail="FOB not found")
    return Response(status_code=204)

@router.delete("/est-ship-dates/{est_ship_date_id}", status_code=204)
def delete_est_ship_date(
    est_ship_date_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete an estimated ship date"""
    service = ReferenceDataService(db)
    success = service.delete_est_ship_date(est_ship_date_id)
    if not success:
        raise HTTPException(status_code=404, detail="Estimated ship date not found")
    return Response(status_code=204)

@router.delete("/est-freights/{est_freight_id}", status_code=204)
def delete_est_freight(
    est_freight_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete an estimated freight"""
    service = ReferenceDataService(db)
    success = service.delete_est_freight(est_freight_id)
    if not success:
        raise HTTPException(status_code=404, detail="Estimated freight not found")
    return Response(status_code=204)

@router.delete("/status-info/{status_info_id}", status_code=204)
def delete_status_info(
    status_info_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a status info"""
    service = ReferenceDataService(db)
    success = service.delete_status_info(status_info_id)
    if not success:
        raise HTTPException(status_code=404, detail="Status info not found")
    return Response(status_code=204)

@router.delete("/priorities/{priority_id}", status_code=204)
def delete_priority(
    priority_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a priority"""
    service = ReferenceDataService(db)
    success = service.delete_priority(priority_id)
    if not success:
        raise HTTPException(status_code=404, detail="Priority not found")
    return Response(status_code=204)

@router.get("/phone-types", response_model=List[PhoneTypeOut])
def get_phone_types(
    active_only: bool = Query(False, description="Return only active records"),
    db: Session = Depends(get_db),
    current_user: Optional[CurrentUser] = Depends(get_current_user_optional)
):
    """Get all phone types"""
    service = ReferenceDataService(db)
    return service.get_phone_types(active_only)

@router.post("/phone-types", response_model=PhoneTypeOut, status_code=201)
def create_phone_type(
    data: PhoneTypeCreateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Create a new phone type"""
    service = ReferenceDataService(db)
    return service.create_phone_type(data)

@router.put("/phone-types/{phone_type_id}", response_model=PhoneTypeOut)
def update_phone_type(
    phone_type_id: int,
    data: PhoneTypeUpdateIn,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Update an existing phone type"""
    service = ReferenceDataService(db)
    result = service.update_phone_type(phone_type_id, data)
    if not result:
        raise HTTPException(status_code=404, detail="Phone type not found")
    return result

@router.delete("/phone-types/{phone_type_id}", status_code=204)
def delete_phone_type(
    phone_type_id: int,
    db: Session = Depends(get_db),
    current_user: CurrentUser = Depends(get_current_user_optional)
):
    """Delete a phone type"""
    service = ReferenceDataService(db)
    success = service.delete_phone_type(phone_type_id)
    if not success:
        raise HTTPException(status_code=404, detail="Phone type not found")
    return Response(status_code=204)