File size: 31,702 Bytes
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1328d27
8879e0b
1328d27
 
 
 
 
7121ebc
1328d27
 
88124cb
 
 
8879e0b
88124cb
8879e0b
c6e89e6
edcf070
 
 
 
4774488
 
 
edcf070
 
 
 
 
 
 
 
 
 
 
6fd3b19
 
 
 
edcf070
 
4774488
 
 
 
 
c91764d
4774488
c91764d
 
edcf070
938b985
 
 
 
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97344b4
 
 
 
4a28cd8
c6e89e6
97344b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edcf070
4774488
edcf070
 
 
 
4a28cd8
 
 
 
 
 
edcf070
4a28cd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edcf070
4a28cd8
 
 
edcf070
 
 
4a28cd8
edcf070
 
 
 
 
4a28cd8
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4774488
1328d27
8879e0b
1328d27
 
 
 
7121ebc
 
 
1328d27
88124cb
 
 
8879e0b
88124cb
8879e0b
c6e89e6
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4774488
edcf070
 
 
 
 
 
 
c91764d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4774488
 
c91764d
4774488
 
 
 
 
 
 
 
 
c91764d
4774488
 
 
 
 
c91764d
 
4774488
 
 
 
c91764d
 
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd3b19
 
 
 
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd3b19
 
 
 
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe79eec
edcf070
 
 
 
 
 
 
 
fe79eec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe79eec
edcf070
 
 
 
 
 
 
 
fe79eec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c1b6f
c1f4954
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88124cb
 
 
 
 
 
8879e0b
88124cb
 
 
 
 
 
8879e0b
 
 
 
 
 
 
88124cb
 
938b985
 
 
 
 
edcf070
938b985
 
 
 
 
 
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
938b985
 
 
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e76e0a5
edcf070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7121ebc
 
 
88124cb
 
 
8879e0b
88124cb
8879e0b
c6e89e6
7121ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88124cb
 
 
 
 
 
 
 
 
 
 
 
8879e0b
 
 
 
 
 
c6e89e6
 
 
 
 
 
 
 
 
 
 
7121ebc
 
 
 
 
 
 
 
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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
package controllers

import (
	"fmt"
	"net/http"
	"strconv"
	"time"

	"service-warungpos-go/config"
	"service-warungpos-go/models"
	"service-warungpos-go/services"

	"github.com/gin-gonic/gin"
)

// DTOs & Custom Structs
type CompanyResponse struct {
	ID                uint       `json:"id"`
	Name              string     `json:"name"`
	BankAccountNumber string     `json:"bank_account_number"`
	BankName          string     `json:"bank_name"`
	Address           string     `json:"address"`
	Email             string     `json:"email"`
	Phone             string     `json:"phone"`
	LogoURL           string     `json:"logo_url"`
	IsActive          bool       `json:"is_active"`
	FeePercentage     float64    `json:"fee_percentage"`
	CashFeeRules       string    `json:"cash_fee_rules"`
	QrisFeeRules       string    `json:"qris_fee_rules"`
	QrisFeePercentage  float64   `json:"qris_fee_percentage"`
	QrisFeeFixed       int       `json:"qris_fee_fixed"`
	QrisFeeMin         int       `json:"qris_fee_min"`
	QrisMinTransaction int       `json:"qris_min_transaction"`
	AllowQrisBelowMin  bool      `json:"allow_qris_below_min"`
	ChargeFeeBeforeDiscount bool `json:"charge_fee_before_discount"`
	WalletBalance      float64   `json:"wallet_balance"`
	WalletLimit        float64   `json:"wallet_limit"`
	EnableMember       bool      `json:"enable_member"`
	EnableVoucher      bool      `json:"enable_voucher"`
	EnableMultiBranch  bool      `json:"enable_multi_branch"`
	MaxBranches        int       `json:"max_branches"`
	EnableMultiKasir   bool      `json:"enable_multi_kasir"`
	MaxCashiers        int       `json:"max_cashiers"`
	EnforceSingleSession bool    `json:"enforce_single_session"`
	ApprovedAt        *time.Time `json:"approved_at"`
	OwnerVerified     *bool      `json:"owner_verified,omitempty"`
	OwnerName         string     `json:"owner_name,omitempty"`
	OwnerEmail        string     `json:"owner_email,omitempty"`
	OwnerPhone        string             `json:"owner_phone,omitempty"`
	PointRules        []models.PointRule `json:"point_rules"`
	CreatedAt         time.Time          `json:"created_at"`
}

type CompanyCreateRequest struct {
	Name              string  `json:"name" binding:"required"`
	BankAccountNumber string  `json:"bank_account_number"`
	BankName          string  `json:"bank_name"`
	Address           string  `json:"address"`
	Email             string  `json:"email"`
	Phone             string  `json:"phone"`
	LogoURL           string  `json:"logo_url"`
	FeePercentage     float64 `json:"fee_percentage"`
	TripayMerchantCode string  `json:"tripay_merchant_code"`
	TripayAPIKey       string  `json:"tripay_api_key"`
	TripayPrivateKey   string  `json:"tripay_private_key"`
	TripayProxyURL     string  `json:"tripay_proxy_url"`
}

type PointRuleInput struct {
	MinAmount   int `json:"min_amount" binding:"required"`
	PointReward int `json:"point_reward" binding:"required"`
}

type UpdateCompanyPointsRequest struct {
	PointRules []PointRuleInput `json:"point_rules"`
}

type BranchCreateRequest struct {
	Name           string `json:"name" binding:"required"`
	Address        string `json:"address"`
	Phone          string `json:"phone"`
	IsPointEnabled *bool  `json:"is_point_enabled"`
}

type BranchResponse struct {
	ID        uint      `json:"id"`
	CompanyID uint      `json:"company_id"`
	Name      string    `json:"name"`
	Address   string    `json:"address"`
	Phone     string    `json:"phone"`
	IsActive  bool      `json:"is_active"`
	CreatedAt time.Time `json:"created_at"`
}

type BranchProductResponse struct {
	ID          uint   `json:"id"`
	Name        string `json:"name"`
	Price       int    `json:"price"`
	CategoryID  *uint  `json:"category_id"`
	ImageURL    string `json:"image_url"`
	IsAvailable bool   `json:"is_available"`
}

// ==================== COMPANY CONTROLLER ====================

func ListCompanies(c *gin.Context) {
	limitStr := c.Query("limit")
	skipStr := c.Query("skip")
	searchQuery := c.Query("search")

	query := config.DB.Model(&models.Company{}).
		Select("id, name, bank_account_number, bank_name, address, email, phone, logo_url, is_active, fee_percentage, cash_fee_rules, qris_fee_rules, qris_fee_percentage, qris_fee_fixed, qris_fee_min, qris_min_transaction, allow_qris_below_min, charge_fee_before_discount, wallet_balance, wallet_limit, enable_member, enable_voucher, enable_multi_branch, max_branches, enable_multi_kasir, max_cashiers, enforce_single_session, approved_at, created_at")

	if searchQuery != "" {
		query = query.Where("name ILIKE ? OR email ILIKE ? OR phone ILIKE ? OR address ILIKE ?", 
			"%"+searchQuery+"%", 
			"%"+searchQuery+"%", 
			"%"+searchQuery+"%",
			"%"+searchQuery+"%")
	}

	if limitStr != "" {
		if limit, err := strconv.Atoi(limitStr); err == nil {
			query = query.Limit(limit)
		}
	}
	if skipStr != "" {
		if skip, err := strconv.Atoi(skipStr); err == nil {
			query = query.Offset(skip)
		}
	}

	var companies []models.Company
	if err := query.Preload("PointRules").Find(&companies).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data perusahaan"})
		return
	}

	if len(companies) == 0 {
		c.JSON(http.StatusOK, []CompanyResponse{})
		return
	}

	var companyIDs []uint
	for _, comp := range companies {
		companyIDs = append(companyIDs, comp.ID)
	}

	// Ambil semua owner user dalam 1 query (hanya kolom yang diperlukan)
	var owners []models.User
	config.DB.Select("id, name, email, phone, company_id").
		Where("company_id IN ? AND role = ?", companyIDs, "owner").
		Find(&owners)

	// Map company_id -> owner
	ownerMap := make(map[uint]models.User)
	var ownerPhones []string
	for _, o := range owners {
		if o.CompanyID != nil {
			ownerMap[*o.CompanyID] = o
			if o.Phone != "" {
				ownerPhones = append(ownerPhones, o.Phone)
			}
		}
	}

	// Ambil status verifikasi OTP semua owner dalam 1 query
	otpMap := make(map[string]bool)
	if len(ownerPhones) > 0 {
		var otps []models.OTP
		config.DB.Select("phone_number").
			Where("phone_number IN ? AND is_verified = ?", ownerPhones, true).
			Find(&otps)
		for _, otp := range otps {
			otpMap[otp.PhoneNumber] = true
		}
	}

	resp := []CompanyResponse{}
	for _, comp := range companies {
		owner, hasOwner := ownerMap[comp.ID]
		var ownerVerified *bool
		var ownerName, ownerEmail, ownerPhone string

		if hasOwner {
			ownerName = owner.Name
			ownerEmail = owner.Email
			ownerPhone = owner.Phone

			if owner.Phone != "" {
				isVerified := otpMap[owner.Phone]
				ownerVerified = &isVerified
			}
		}

		resp = append(resp, CompanyResponse{
			ID:                comp.ID,
			Name:              comp.Name,
			BankAccountNumber: comp.BankAccountNumber,
			BankName:          comp.BankName,
			Address:           comp.Address,
			Email:             comp.Email,
			Phone:             comp.Phone,
			LogoURL:           comp.LogoURL,
			IsActive:          comp.IsActive,
			FeePercentage:     comp.FeePercentage,
			ApprovedAt:        comp.ApprovedAt,
			OwnerVerified:     ownerVerified,
			OwnerName:         ownerName,
			OwnerEmail:        ownerEmail,
			OwnerPhone:        ownerPhone,
			PointRules:        comp.PointRules,
			CashFeeRules:      comp.CashFeeRules,
			QrisFeeRules:      comp.QrisFeeRules,
			QrisFeePercentage: comp.QrisFeePercentage,
			QrisFeeFixed:      comp.QrisFeeFixed,
			QrisFeeMin:        comp.QrisFeeMin,
			QrisMinTransaction: comp.QrisMinTransaction,
			AllowQrisBelowMin:  comp.AllowQrisBelowMin,
			ChargeFeeBeforeDiscount: comp.ChargeFeeBeforeDiscount,
			WalletBalance:      comp.WalletBalance,
			WalletLimit:       comp.WalletLimit,
			EnableMember:      comp.EnableMember,
			EnableVoucher:     comp.EnableVoucher,
			EnableMultiBranch: comp.EnableMultiBranch,
			MaxBranches:       comp.MaxBranches,
			EnableMultiKasir:  comp.EnableMultiKasir,
			MaxCashiers:       comp.MaxCashiers,
			EnforceSingleSession: comp.EnforceSingleSession,
			CreatedAt:         comp.CreatedAt,
		})
	}

	c.JSON(http.StatusOK, resp)
}

func GetMyCompany(c *gin.Context) {
	userVal, _ := c.Get("user")
	user := userVal.(*models.User)

	if user.CompanyID == nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Tidak terhubung ke perusahaan"})
		return
	}

	var company models.Company
	if err := config.DB.Preload("PointRules").First(&company, *user.CompanyID).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Perusahaan tidak ditemukan"})
		return
	}

	c.JSON(http.StatusOK, company)
}

func UpdateMyCompanyPoints(c *gin.Context) {
	userVal, exists := c.Get("user")
	if !exists {
		c.JSON(http.StatusUnauthorized, gin.H{"detail": "Unauthorized"})
		return
	}
	user := userVal.(*models.User)

	if user.CompanyID == nil || user.Role != "owner" {
		c.JSON(http.StatusForbidden, gin.H{"detail": "Hanya owner yang bisa mengatur poin"})
		return
	}

	var req UpdateCompanyPointsRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "Data tidak valid"})
		return
	}
	// Hapus point rules yang lama
	config.DB.Where("company_id = ?", *user.CompanyID).Delete(&models.PointRule{})

	// Masukkan point rules yang baru
	var newRules []models.PointRule
	for _, r := range req.PointRules {
		newRules = append(newRules, models.PointRule{
			CompanyID:   *user.CompanyID,
			MinAmount:   r.MinAmount,
			PointReward: r.PointReward,
		})
	}

	if len(newRules) > 0 {
		if err := config.DB.Create(&newRules).Error; err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menyimpan pengaturan poin"})
			return
		}
	}

	var updatedCompany models.Company
	config.DB.Preload("PointRules").First(&updatedCompany, *user.CompanyID)

	c.JSON(http.StatusOK, gin.H{"message": "Pengaturan poin berhasil disimpan", "company": updatedCompany})
}

func GetCompany(c *gin.Context) {
	companyIDStr := c.Param("company_id")
	companyID, err := strconv.Atoi(companyIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID perusahaan tidak valid"})
		return
	}

	var company models.Company
	if err := config.DB.First(&company, companyID).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Company not found"})
		return
	}

	c.JSON(http.StatusOK, company)
}

func CreateCompany(c *gin.Context) {
	var req CompanyCreateRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
		return
	}

	var count int64
	config.DB.Model(&models.Company{}).Where("name = ?", req.Name).Count(&count)
	if count > 0 {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "Company name already exists"})
		return
	}

	newComp := models.Company{
		Name:              req.Name,
		BankAccountNumber: req.BankAccountNumber,
		BankName:          req.BankName,
		Address:           req.Address,
		Email:             req.Email,
		Phone:             req.Phone,
		LogoURL:           req.LogoURL,
		IsActive:          true,
		FeePercentage:     req.FeePercentage,
		TripayMerchantCode: req.TripayMerchantCode,
		TripayAPIKey:       req.TripayAPIKey,
		TripayPrivateKey:   req.TripayPrivateKey,
		TripayProxyURL:     req.TripayProxyURL,
	}

	if err := config.DB.Create(&newComp).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal membuat perusahaan baru"})
		return
	}

	c.JSON(http.StatusCreated, newComp)
}

func UpdateCompany(c *gin.Context) {
	companyIDStr := c.Param("company_id")
	companyID, err := strconv.Atoi(companyIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID perusahaan tidak valid"})
		return
	}

	var req CompanyCreateRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
		return
	}

	var company models.Company
	if err := config.DB.First(&company, companyID).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Company not found"})
		return
	}

	if req.Name != "" {
		var count int64
		config.DB.Model(&models.Company{}).Where("name = ? AND id != ?", req.Name, companyID).Count(&count)
		if count > 0 {
			c.JSON(http.StatusBadRequest, gin.H{"detail": "Company name already exists"})
			return
		}
		company.Name = req.Name
	}

	company.BankAccountNumber = req.BankAccountNumber
	company.BankName = req.BankName
	company.Address = req.Address
	company.Email = req.Email
	company.Phone = req.Phone
	company.LogoURL = req.LogoURL
	company.FeePercentage = req.FeePercentage
	company.TripayMerchantCode = req.TripayMerchantCode
	company.TripayAPIKey = req.TripayAPIKey
	company.TripayPrivateKey = req.TripayPrivateKey
	company.TripayProxyURL = req.TripayProxyURL

	if err := config.DB.Save(&company).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal meng-update perusahaan"})
		return
	}

	c.JSON(http.StatusOK, company)
}

func ToggleActiveCompany(c *gin.Context) {
	companyIDStr := c.Param("company_id")
	companyID, err := strconv.Atoi(companyIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID perusahaan tidak valid"})
		return
	}

	var company models.Company
	if err := config.DB.First(&company, companyID).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Company not found"})
		return
	}

	tx := config.DB.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	company.IsActive = !company.IsActive
	tx.Save(&company)

	// Ubah status keaktifan owner juga
	var owner models.User
	if err := tx.Where("company_id = ? AND role = ?", companyID, "owner").First(&owner).Error; err == nil {
		owner.IsActive = company.IsActive
		tx.Save(&owner)
	}

	if err := tx.Commit().Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal melakukan toggle status"})
		return
	}

	status := "dinonaktifkan"
	if company.IsActive {
		status = "diaktifkan"
	}

	c.JSON(http.StatusOK, gin.H{
		"message":   fmt.Sprintf("Perusahaan %s berhasil %s", company.Name, status),
		"is_active": company.IsActive,
	})
}

func DeleteCompany(c *gin.Context) {
	companyIDStr := c.Param("company_id")
	companyID, err := strconv.Atoi(companyIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID perusahaan tidak valid"})
		return
	}

	var company models.Company
	if err := config.DB.First(&company, companyID).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Company not found"})
		return
	}

	tx := config.DB.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	// 1. Ambil semua user terkait company ini, lalu hapus record OTP mereka
	var users []models.User
	tx.Where("company_id = ?", companyID).Find(&users)
	for _, u := range users {
		if u.Phone != "" {
			tx.Where("phone_number = ?", u.Phone).Delete(&models.OTP{})
		}
	}

	// 2. Hapus order_items terlebih dahulu (menggunakan subquery) untuk menghindari FK violations
	if err := tx.Exec("DELETE FROM order_items WHERE order_id IN (SELECT id FROM orders WHERE company_id = ?)", companyID).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data order items"})
		return
	}

	// 3. Hapus orders
	if err := tx.Where("company_id = ?", companyID).Delete(&models.Order{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data order"})
		return
	}

	// 4. Hapus branch_products
	if err := tx.Exec("DELETE FROM branch_products WHERE product_id IN (SELECT id FROM products WHERE company_id = ?)", companyID).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data branch products"})
		return
	}

	// 5. Hapus products
	if err := tx.Where("company_id = ?", companyID).Delete(&models.Product{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data produk"})
		return
	}

	// 6. Hapus categories
	if err := tx.Where("company_id = ?", companyID).Delete(&models.Category{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data kategori"})
		return
	}

	// 7. Hapus users
	if err := tx.Where("company_id = ?", companyID).Delete(&models.User{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data user"})
		return
	}

	// 8. Hapus branches
	if err := tx.Where("company_id = ?", companyID).Delete(&models.Branch{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data cabang"})
		return
	}

	// 9. Terakhir hapus company utama
	if err := tx.Delete(&company).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data perusahaan"})
		return
	}

	tx.Commit()

	c.JSON(http.StatusOK, gin.H{
		"message": fmt.Sprintf("Perusahaan '%s' dan seluruh data terkait berhasil dihapus", company.Name),
	})
}

func RejectCompany(c *gin.Context) {
	companyIDStr := c.Param("company_id")
	companyID, err := strconv.Atoi(companyIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID perusahaan tidak valid"})
		return
	}

	var company models.Company
	if err := config.DB.First(&company, companyID).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Company not found"})
		return
	}

	if company.IsActive {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "Tidak bisa menolak perusahaan yang sudah aktif"})
		return
	}

	tx := config.DB.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	// 1. Ambil semua user terkait company ini, lalu hapus record OTP mereka
	var users []models.User
	tx.Where("company_id = ?", companyID).Find(&users)
	for _, u := range users {
		if u.Phone != "" {
			tx.Where("phone_number = ?", u.Phone).Delete(&models.OTP{})
		}
	}

	// 2. Hapus order_items terlebih dahulu (menggunakan subquery)
	if err := tx.Exec("DELETE FROM order_items WHERE order_id IN (SELECT id FROM orders WHERE company_id = ?)", companyID).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data order items"})
		return
	}

	// 3. Hapus orders
	if err := tx.Where("company_id = ?", companyID).Delete(&models.Order{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data order"})
		return
	}

	// 4. Hapus branch_products
	if err := tx.Exec("DELETE FROM branch_products WHERE product_id IN (SELECT id FROM products WHERE company_id = ?)", companyID).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data branch products"})
		return
	}

	// 5. Hapus products
	if err := tx.Where("company_id = ?", companyID).Delete(&models.Product{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data produk"})
		return
	}

	// 6. Hapus categories
	if err := tx.Where("company_id = ?", companyID).Delete(&models.Category{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data kategori"})
		return
	}

	// 7. Hapus users
	if err := tx.Where("company_id = ?", companyID).Delete(&models.User{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data user"})
		return
	}

	// 8. Hapus branches
	if err := tx.Where("company_id = ?", companyID).Delete(&models.Branch{}).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data cabang"})
		return
	}

	// 9. Terakhir hapus company utama
	if err := tx.Delete(&company).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menghapus data perusahaan"})
		return
	}

	tx.Commit()

	c.JSON(http.StatusOK, gin.H{
		"message": fmt.Sprintf("Pendaftaran '%s' ditolak dan dihapus dari sistem", company.Name),
	})
}

func ApproveCompany(c *gin.Context) {
	companyIDStr := c.Param("company_id")
	companyID, err := strconv.Atoi(companyIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID perusahaan tidak valid"})
		return
	}

	tx := config.DB.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	var company models.Company
	if err := tx.First(&company, companyID).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusNotFound, gin.H{"detail": "Company not found"})
		return
	}

	var owner models.User
	if err := tx.Where("company_id = ? AND role = ?", companyID, "owner").First(&owner).Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusBadRequest, gin.H{"detail": "Company ini belum memiliki Owner"})
		return
	}

	now := time.Now()
	company.IsActive = true
	company.ApprovedAt = &now
	tx.Save(&company)

	owner.IsActive = true
	tx.Save(&owner)

	if err := tx.Commit().Error; err != nil {
		tx.Rollback()
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menyetujui perusahaan"})
		return
	}

	// Kirim Notifikasi WA (Async)
	go services.SendApprovalWa(owner.Phone, company.Name, owner.Email)

	c.JSON(http.StatusOK, gin.H{
		"message": "Perusahaan dan owner berhasil disetujui",
	})
}

// ==================== BRANCH CONTROLLER ====================

func ListBranches(c *gin.Context) {
	userVal, _ := c.Get("user")
	owner := userVal.(*models.User)

	branches := []models.Branch{}
	if err := config.DB.Select("id, company_id, name, address, phone, is_active, is_point_enabled, created_at").Where("company_id = ?", *owner.CompanyID).Find(&branches).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data cabang"})
		return
	}

	c.JSON(http.StatusOK, branches)
}

func CreateBranch(c *gin.Context) {
	userVal, _ := c.Get("user")
	owner := userVal.(*models.User)

	var req BranchCreateRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
		return
	}

	var count int64
	config.DB.Model(&models.Branch{}).Where("company_id = ? AND name = ?", *owner.CompanyID, req.Name).Count(&count)
	if count > 0 {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "Nama cabang sudah ada"})
		return
	}

	var company models.Company
	if err := config.DB.First(&company, *owner.CompanyID).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data perusahaan"})
		return
	}

	if !company.EnableMultiBranch && company.MaxBranches <= 0 {
		var branchCount int64
		config.DB.Model(&models.Branch{}).Where("company_id = ?", *owner.CompanyID).Count(&branchCount)
		if branchCount >= 1 {
			c.JSON(http.StatusForbidden, gin.H{"detail": "Paket perusahaan Anda tidak mendukung pembuatan lebih dari 1 cabang"})
			return
		}
	} else if company.MaxBranches > 0 {
		var branchCount int64
		config.DB.Model(&models.Branch{}).Where("company_id = ?", *owner.CompanyID).Count(&branchCount)
		if branchCount >= int64(company.MaxBranches) {
			c.JSON(http.StatusForbidden, gin.H{"detail": fmt.Sprintf("Batas maksimal cabang (%d) telah tercapai untuk perusahaan Anda", company.MaxBranches)})
			return
		}
	}

	isPointEnabled := true
	if req.IsPointEnabled != nil {
		isPointEnabled = *req.IsPointEnabled
	}

	newBranch := models.Branch{
		CompanyID:      *owner.CompanyID,
		Name:           req.Name,
		Address:        req.Address,
		Phone:          req.Phone,
		IsActive:       true,
		IsPointEnabled: isPointEnabled,
	}

	if err := config.DB.Create(&newBranch).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menyimpan cabang baru"})
		return
	}

	c.JSON(http.StatusCreated, newBranch)
}

func UpdateBranch(c *gin.Context) {
	userVal, _ := c.Get("user")
	owner := userVal.(*models.User)

	branchIDStr := c.Param("branch_id")
	branchID, err := strconv.Atoi(branchIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID cabang tidak valid"})
		return
	}

	var req BranchCreateRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
		return
	}

	var branch models.Branch
	if err := config.DB.Where("id = ? AND company_id = ?", branchID, *owner.CompanyID).First(&branch).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Cabang tidak ditemukan"})
		return
	}

	if req.Name != "" {
		var count int64
		config.DB.Model(&models.Branch{}).Where("company_id = ? AND name = ? AND id != ?", *owner.CompanyID, req.Name, branchID).Count(&count)
		if count > 0 {
			c.JSON(http.StatusBadRequest, gin.H{"detail": "Nama cabang sudah ada"})
			return
		}
		branch.Name = req.Name
	}

	branch.Address = req.Address
	branch.Phone = req.Phone
	if req.IsPointEnabled != nil {
		branch.IsPointEnabled = *req.IsPointEnabled
	}

	if err := config.DB.Save(&branch).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal meng-update cabang"})
		return
	}

	c.JSON(http.StatusOK, branch)
}

func DeleteBranch(c *gin.Context) {
	userVal, _ := c.Get("user")
	owner := userVal.(*models.User)

	branchIDStr := c.Param("branch_id")
	branchID, err := strconv.Atoi(branchIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID cabang tidak valid"})
		return
	}

	var branch models.Branch
	if err := config.DB.Where("id = ? AND company_id = ?", branchID, *owner.CompanyID).First(&branch).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Cabang tidak ditemukan"})
		return
	}

	branch.IsActive = false
	config.DB.Save(&branch)

	c.JSON(http.StatusOK, gin.H{"message": "Cabang dinonaktifkan"})
}

func ToggleBranchStatus(c *gin.Context) {
	userVal, _ := c.Get("user")
	owner := userVal.(*models.User)

	branchIDStr := c.Param("branch_id")
	branchID, err := strconv.Atoi(branchIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID cabang tidak valid"})
		return
	}

	var branch models.Branch
	if err := config.DB.Where("id = ? AND company_id = ?", branchID, *owner.CompanyID).First(&branch).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Cabang tidak ditemukan"})
		return
	}

	branch.IsActive = !branch.IsActive
	config.DB.Save(&branch)

	status := "dinonaktifkan"
	if branch.IsActive {
		status = "diaktifkan"
	}

	c.JSON(http.StatusOK, gin.H{
		"message":   fmt.Sprintf("Cabang berhasil %s", status),
		"is_active": branch.IsActive,
	})
}

func GetBranchProducts(c *gin.Context) {
	userVal, _ := c.Get("user")
	owner := userVal.(*models.User)

	branchIDStr := c.Param("branch_id")
	branchID, err := strconv.Atoi(branchIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID cabang tidak valid"})
		return
	}

	// Pastikan cabang milik perusahaan owner
	var branch models.Branch
	if err := config.DB.Where("id = ? AND company_id = ?", branchID, *owner.CompanyID).First(&branch).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Cabang tidak ditemukan"})
		return
	}

	// Ambil semua produk aktif
	var products []models.Product
	if err := config.DB.Where("company_id = ? AND is_active = ?", *owner.CompanyID, true).Find(&products).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal mengambil data produk"})
		return
	}

	// Ambil relasi availability
	var branchProducts []models.BranchProduct
	config.DB.Where("branch_id = ?", branchID).Find(&branchProducts)

	bpMap := make(map[uint]bool)
	for _, bp := range branchProducts {
		bpMap[bp.ProductID] = bp.IsAvailable
	}
	resp := []BranchProductResponse{}
	for _, p := range products {
		isAvailable, exists := bpMap[p.ID]
		if !exists {
			isAvailable = true // Default tersedia
		}

		resp = append(resp, BranchProductResponse{
			ID:          p.ID,
			Name:        p.Name,
			Price:       p.Price,
			CategoryID:  p.CategoryID,
			ImageURL:    p.ImageURL,
			IsAvailable: isAvailable,
		})
	}

	c.JSON(http.StatusOK, resp)
}

func ToggleProductAvailability(c *gin.Context) {
	userVal, _ := c.Get("user")
	owner := userVal.(*models.User)

	branchIDStr := c.Param("branch_id")
	branchID, err := strconv.Atoi(branchIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID cabang tidak valid"})
		return
	}

	productIDStr := c.Param("product_id")
	productID, err := strconv.Atoi(productIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID produk tidak valid"})
		return
	}

	var branch models.Branch
	if err := config.DB.Where("id = ? AND company_id = ?", branchID, *owner.CompanyID).First(&branch).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Cabang tidak ditemukan"})
		return
	}

	var product models.Product
	if err := config.DB.Where("id = ? AND company_id = ?", productID, *owner.CompanyID).First(&product).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Produk tidak ditemukan"})
		return
	}

	var bp models.BranchProduct
	err = config.DB.Where("branch_id = ? AND product_id = ?", branchID, productID).First(&bp).Error

	if err == nil {
		bp.IsAvailable = !bp.IsAvailable
		config.DB.Save(&bp)
	} else {
		// Belum pernah diset -> default True, jadi toggle = False
		bp = models.BranchProduct{
			BranchID:    uint(branchID),
			ProductID:   uint(productID),
			IsAvailable: false,
		}
		config.DB.Create(&bp)
	}

	c.JSON(http.StatusOK, gin.H{
		"message":      "Status diperbarui",
		"is_available": bp.IsAvailable,
	})
}

type UpdateCompanySettingsRequest struct {
	ChargeFeeBeforeDiscount *bool `json:"charge_fee_before_discount"`
	EnableMember            *bool `json:"enable_member"`
	EnableVoucher           *bool `json:"enable_voucher"`
	EnableMultiBranch       *bool `json:"enable_multi_branch"`
	MaxBranches             *int  `json:"max_branches"`
	EnableMultiKasir        *bool `json:"enable_multi_kasir"`
	MaxCashiers             *int  `json:"max_cashiers"`
	EnforceSingleSession    *bool `json:"enforce_single_session"`
}

func AdminUpdateCompanySettings(c *gin.Context) {
	companyIDStr := c.Param("id")
	companyID, err := strconv.Atoi(companyIDStr)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": "ID Perusahaan tidak valid"})
		return
	}

	var req UpdateCompanySettingsRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"detail": err.Error()})
		return
	}

	var company models.Company
	if err := config.DB.First(&company, companyID).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"detail": "Perusahaan tidak ditemukan"})
		return
	}

	if req.ChargeFeeBeforeDiscount != nil {
		company.ChargeFeeBeforeDiscount = *req.ChargeFeeBeforeDiscount
	}
	if req.EnableMember != nil {
		company.EnableMember = *req.EnableMember
	}
	if req.EnableVoucher != nil {
		company.EnableVoucher = *req.EnableVoucher
	}
	if req.EnableMultiBranch != nil {
		company.EnableMultiBranch = *req.EnableMultiBranch
	}
	if req.EnableMultiKasir != nil {
		company.EnableMultiKasir = *req.EnableMultiKasir
	}
	if req.MaxBranches != nil {
		company.MaxBranches = *req.MaxBranches
	}
	if req.MaxCashiers != nil {
		company.MaxCashiers = *req.MaxCashiers
	}
	if req.EnforceSingleSession != nil {
		company.EnforceSingleSession = *req.EnforceSingleSession
		// Jika fitur dimatikan, hapus semua sesi aktif kasir perusahaan ini
		if !*req.EnforceSingleSession {
			var kasirIDs []uint
			config.DB.Model(&models.User{}).Where("company_id = ? AND role = ?", company.ID, "kasir").Pluck("id", &kasirIDs)
			if len(kasirIDs) > 0 {
				config.DB.Where("user_id IN ?", kasirIDs).Delete(&models.UserSession{})
			}
		}
	}

	if err := config.DB.Save(&company).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal menyimpan pengaturan"})
		return
	}

	c.JSON(http.StatusOK, company)
}