summaryrefslogtreecommitdiff
path: root/internal/queries/queries.sql.go
blob: 035be20d32542333d4d003819c53067ff2057be0 (plain)
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
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: queries.sql

package queries

import (
	"context"
	"database/sql"
	"time"
)

const archiveClient = `-- name: ArchiveClient :exec
update client
set archived = 1
where id = ?1
`

func (q *Queries) ArchiveClient(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, archiveClient, id)
	return err
}

const archiveProject = `-- name: ArchiveProject :exec
update project
set archived = 1
where id = ?1
`

func (q *Queries) ArchiveProject(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, archiveProject, id)
	return err
}

const createClient = `-- name: CreateClient :one
insert into client (name, email, billable_rate)
values (?1, ?2, ?3)
returning id, name, email, billable_rate, archived, created_at
`

type CreateClientParams struct {
	Name         string
	Email        sql.NullString
	BillableRate sql.NullInt64
}

func (q *Queries) CreateClient(ctx context.Context, arg CreateClientParams) (Client, error) {
	row := q.db.QueryRowContext(ctx, createClient, arg.Name, arg.Email, arg.BillableRate)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Email,
		&i.BillableRate,
		&i.Archived,
		&i.CreatedAt,
	)
	return i, err
}

const createContractor = `-- name: CreateContractor :one
insert into contractor (name, label, email)
values (?1, ?2, ?3)
returning id, name, label, email, created_at
`

type CreateContractorParams struct {
	Name  string
	Label string
	Email string
}

func (q *Queries) CreateContractor(ctx context.Context, arg CreateContractorParams) (Contractor, error) {
	row := q.db.QueryRowContext(ctx, createContractor, arg.Name, arg.Label, arg.Email)
	var i Contractor
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Label,
		&i.Email,
		&i.CreatedAt,
	)
	return i, err
}

const createInvoice = `-- name: CreateInvoice :one
insert into invoice (year, month, number, client_id, total_amount)
values (?1, ?2, ?3, ?4, ?5)
returning id, year, month, number, client_id, total_amount, created_at
`

type CreateInvoiceParams struct {
	Year        int64
	Month       int64
	Number      int64
	ClientID    int64
	TotalAmount int64
}

func (q *Queries) CreateInvoice(ctx context.Context, arg CreateInvoiceParams) (Invoice, error) {
	row := q.db.QueryRowContext(ctx, createInvoice,
		arg.Year,
		arg.Month,
		arg.Number,
		arg.ClientID,
		arg.TotalAmount,
	)
	var i Invoice
	err := row.Scan(
		&i.ID,
		&i.Year,
		&i.Month,
		&i.Number,
		&i.ClientID,
		&i.TotalAmount,
		&i.CreatedAt,
	)
	return i, err
}

const createProject = `-- name: CreateProject :one
insert into project (name, client_id, billable_rate)
values (?1, ?2, ?3)
returning id, name, client_id, billable_rate, archived, created_at
`

type CreateProjectParams struct {
	Name         string
	ClientID     int64
	BillableRate sql.NullInt64
}

func (q *Queries) CreateProject(ctx context.Context, arg CreateProjectParams) (Project, error) {
	row := q.db.QueryRowContext(ctx, createProject, arg.Name, arg.ClientID, arg.BillableRate)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.ClientID,
		&i.BillableRate,
		&i.Archived,
		&i.CreatedAt,
	)
	return i, err
}

const createTimeEntry = `-- name: CreateTimeEntry :one
insert into time_entry (start_time, description, client_id, project_id, billable_rate)
values (
    datetime('now', 'utc'), 
    ?1, 
    ?2, 
    ?3,
    ?4
)
returning id, start_time, end_time, description, client_id, project_id, billable_rate
`

type CreateTimeEntryParams struct {
	Description  sql.NullString
	ClientID     int64
	ProjectID    sql.NullInt64
	BillableRate sql.NullInt64
}

func (q *Queries) CreateTimeEntry(ctx context.Context, arg CreateTimeEntryParams) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, createTimeEntry,
		arg.Description,
		arg.ClientID,
		arg.ProjectID,
		arg.BillableRate,
	)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const createTimeEntryWithTimes = `-- name: CreateTimeEntryWithTimes :one
insert into time_entry (start_time, end_time, description, client_id, project_id, billable_rate)
values (
    datetime(?1), 
    datetime(?2), 
    ?3, 
    ?4, 
    ?5,
    ?6
)
returning id, start_time, end_time, description, client_id, project_id, billable_rate
`

type CreateTimeEntryWithTimesParams struct {
	StartTime    interface{}
	EndTime      interface{}
	Description  sql.NullString
	ClientID     int64
	ProjectID    sql.NullInt64
	BillableRate sql.NullInt64
}

func (q *Queries) CreateTimeEntryWithTimes(ctx context.Context, arg CreateTimeEntryWithTimesParams) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, createTimeEntryWithTimes,
		arg.StartTime,
		arg.EndTime,
		arg.Description,
		arg.ClientID,
		arg.ProjectID,
		arg.BillableRate,
	)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const editTimeEntry = `-- name: EditTimeEntry :exec
update time_entry
set
	start_time = datetime(?1),
	end_time = datetime(?2),
	description = ?3,
	client_id = ?4,
	project_id = ?5,
	billable_rate = ?6
where id = ?7
`

type EditTimeEntryParams struct {
	StartTime   interface{}
	EndTime     interface{}
	Description sql.NullString
	ClientID    int64
	ProjectID   sql.NullInt64
	HourlyRate  sql.NullInt64
	EntryID     int64
}

func (q *Queries) EditTimeEntry(ctx context.Context, arg EditTimeEntryParams) error {
	_, err := q.db.ExecContext(ctx, editTimeEntry,
		arg.StartTime,
		arg.EndTime,
		arg.Description,
		arg.ClientID,
		arg.ProjectID,
		arg.HourlyRate,
		arg.EntryID,
	)
	return err
}

const findClient = `-- name: FindClient :many
select c1.id, c1.name, c1.email, c1.billable_rate, c1.archived, c1.created_at from client c1 where c1.id = cast(?1 as integer)
union all
select c2.id, c2.name, c2.email, c2.billable_rate, c2.archived, c2.created_at from client c2 where c2.name = ?2
`

type FindClientParams struct {
	ID   int64
	Name string
}

func (q *Queries) FindClient(ctx context.Context, arg FindClientParams) ([]Client, error) {
	rows, err := q.db.QueryContext(ctx, findClient, arg.ID, arg.Name)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Client
	for rows.Next() {
		var i Client
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Email,
			&i.BillableRate,
			&i.Archived,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const findProject = `-- name: FindProject :many
select p1.id, p1.name, p1.client_id, p1.billable_rate, p1.archived, p1.created_at from project p1 where p1.id = cast(?1 as integer)
union all
select p2.id, p2.name, p2.client_id, p2.billable_rate, p2.archived, p2.created_at from project p2 where p2.name = ?2
`

type FindProjectParams struct {
	ID   int64
	Name string
}

func (q *Queries) FindProject(ctx context.Context, arg FindProjectParams) ([]Project, error) {
	rows, err := q.db.QueryContext(ctx, findProject, arg.ID, arg.Name)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Project
	for rows.Next() {
		var i Project
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.ClientID,
			&i.BillableRate,
			&i.Archived,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getActiveTimeEntry = `-- name: GetActiveTimeEntry :one
select id, start_time, end_time, description, client_id, project_id, billable_rate from time_entry 
where end_time is null
order by start_time desc
limit 1
`

func (q *Queries) GetActiveTimeEntry(ctx context.Context) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, getActiveTimeEntry)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const getClientByName = `-- name: GetClientByName :one
select id, name, email, billable_rate, archived, created_at from client where name = ?1 limit 1
`

func (q *Queries) GetClientByName(ctx context.Context, name string) (Client, error) {
	row := q.db.QueryRowContext(ctx, getClientByName, name)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Email,
		&i.BillableRate,
		&i.Archived,
		&i.CreatedAt,
	)
	return i, err
}

const getContractor = `-- name: GetContractor :one
select id, name, label, email, created_at from contractor 
order by id 
limit 1
`

func (q *Queries) GetContractor(ctx context.Context) (Contractor, error) {
	row := q.db.QueryRowContext(ctx, getContractor)
	var i Contractor
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Label,
		&i.Email,
		&i.CreatedAt,
	)
	return i, err
}

const getFilteredTimeEntries = `-- name: GetFilteredTimeEntries :many
select id, start_time, end_time, description, client_id, project_id, billable_rate from time_entry 
where start_time >= ?1
  and (?2 is null or start_time <= ?2)
  and (?3 is null or client_id = ?3)
  and (?4 is null or project_id = ?4)
order by start_time desc
`

type GetFilteredTimeEntriesParams struct {
	StartTime time.Time
	EndTime   interface{}
	ClientID  interface{}
	ProjectID interface{}
}

func (q *Queries) GetFilteredTimeEntries(ctx context.Context, arg GetFilteredTimeEntriesParams) ([]TimeEntry, error) {
	rows, err := q.db.QueryContext(ctx, getFilteredTimeEntries,
		arg.StartTime,
		arg.EndTime,
		arg.ClientID,
		arg.ProjectID,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []TimeEntry
	for rows.Next() {
		var i TimeEntry
		if err := rows.Scan(
			&i.ID,
			&i.StartTime,
			&i.EndTime,
			&i.Description,
			&i.ClientID,
			&i.ProjectID,
			&i.BillableRate,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getHighestInvoiceNumber = `-- name: GetHighestInvoiceNumber :one
select cast(coalesce(max(number), 0) as integer) as max_number
from invoice
where year = ?1 and month = ?2
`

type GetHighestInvoiceNumberParams struct {
	Year  int64
	Month int64
}

func (q *Queries) GetHighestInvoiceNumber(ctx context.Context, arg GetHighestInvoiceNumberParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, getHighestInvoiceNumber, arg.Year, arg.Month)
	var max_number int64
	err := row.Scan(&max_number)
	return max_number, err
}

const getInvoiceDataByClient = `-- name: GetInvoiceDataByClient :many
select
    te.id as time_entry_id,
    te.start_time,
    te.end_time,
    te.description,
    te.billable_rate as entry_billable_rate,
    c.id as client_id,
    c.name as client_name,
    c.billable_rate as client_billable_rate,
    p.id as project_id,
    p.name as project_name,
    p.billable_rate as project_billable_rate,
    cast(round((julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60) as integer) as duration_seconds,
    case
        when te.billable_rate is not null then 'entry'
        when p.billable_rate is not null then 'project'
        else 'client'
    end as rate_source
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where c.id = ?1
    and te.start_time >= ?2
    and te.start_time <= ?3
    and te.end_time is not null
order by te.start_time
`

type GetInvoiceDataByClientParams struct {
	ClientID  int64
	StartTime time.Time
	EndTime   time.Time
}

type GetInvoiceDataByClientRow struct {
	TimeEntryID         int64
	StartTime           time.Time
	EndTime             sql.NullTime
	Description         sql.NullString
	EntryBillableRate   sql.NullInt64
	ClientID            int64
	ClientName          string
	ClientBillableRate  sql.NullInt64
	ProjectID           sql.NullInt64
	ProjectName         sql.NullString
	ProjectBillableRate sql.NullInt64
	DurationSeconds     int64
	RateSource          string
}

func (q *Queries) GetInvoiceDataByClient(ctx context.Context, arg GetInvoiceDataByClientParams) ([]GetInvoiceDataByClientRow, error) {
	rows, err := q.db.QueryContext(ctx, getInvoiceDataByClient, arg.ClientID, arg.StartTime, arg.EndTime)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetInvoiceDataByClientRow
	for rows.Next() {
		var i GetInvoiceDataByClientRow
		if err := rows.Scan(
			&i.TimeEntryID,
			&i.StartTime,
			&i.EndTime,
			&i.Description,
			&i.EntryBillableRate,
			&i.ClientID,
			&i.ClientName,
			&i.ClientBillableRate,
			&i.ProjectID,
			&i.ProjectName,
			&i.ProjectBillableRate,
			&i.DurationSeconds,
			&i.RateSource,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getInvoiceDataByProject = `-- name: GetInvoiceDataByProject :many
select 
    te.id as time_entry_id,
    te.start_time,
    te.end_time,
    te.description,
    te.billable_rate as entry_billable_rate,
    c.id as client_id,
    c.name as client_name,
    c.billable_rate as client_billable_rate,
    p.id as project_id,
    p.name as project_name,
    p.billable_rate as project_billable_rate,
    cast(
        case 
            when te.end_time is null then 
                (julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
            else 
                (julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
        end as integer
    ) as duration_seconds,
    case
        when te.billable_rate is not null then 'entry'
        when p.billable_rate is not null then 'project'
        else 'client'
    end as rate_source
from time_entry te
join client c on te.client_id = c.id
join project p on te.project_id = p.id
where p.id = ?1
    and te.start_time >= ?2
    and te.start_time <= ?3
    and te.end_time is not null
order by te.start_time
`

type GetInvoiceDataByProjectParams struct {
	ProjectID int64
	StartTime time.Time
	EndTime   time.Time
}

type GetInvoiceDataByProjectRow struct {
	TimeEntryID         int64
	StartTime           time.Time
	EndTime             sql.NullTime
	Description         sql.NullString
	EntryBillableRate   sql.NullInt64
	ClientID            int64
	ClientName          string
	ClientBillableRate  sql.NullInt64
	ProjectID           int64
	ProjectName         string
	ProjectBillableRate sql.NullInt64
	DurationSeconds     int64
	RateSource          string
}

func (q *Queries) GetInvoiceDataByProject(ctx context.Context, arg GetInvoiceDataByProjectParams) ([]GetInvoiceDataByProjectRow, error) {
	rows, err := q.db.QueryContext(ctx, getInvoiceDataByProject, arg.ProjectID, arg.StartTime, arg.EndTime)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetInvoiceDataByProjectRow
	for rows.Next() {
		var i GetInvoiceDataByProjectRow
		if err := rows.Scan(
			&i.TimeEntryID,
			&i.StartTime,
			&i.EndTime,
			&i.Description,
			&i.EntryBillableRate,
			&i.ClientID,
			&i.ClientName,
			&i.ClientBillableRate,
			&i.ProjectID,
			&i.ProjectName,
			&i.ProjectBillableRate,
			&i.DurationSeconds,
			&i.RateSource,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getMonthSummaryByProject = `-- name: GetMonthSummaryByProject :many
select 
    p.id as project_id,
    p.name as project_name,
    c.id as client_id,
    c.name as client_name,
    cast(sum(
        case 
            when te.end_time is null then 
                (julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
            else 
                (julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
        end
    ) as integer) as total_seconds
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where date(te.start_time, 'localtime') >= date('now', 'localtime', 'start of month')
    and date(te.start_time) <= date('now', 'utc')
group by p.id, p.name, c.id, c.name
order by c.name, p.name
`

type GetMonthSummaryByProjectRow struct {
	ProjectID    sql.NullInt64
	ProjectName  sql.NullString
	ClientID     int64
	ClientName   string
	TotalSeconds int64
}

func (q *Queries) GetMonthSummaryByProject(ctx context.Context) ([]GetMonthSummaryByProjectRow, error) {
	rows, err := q.db.QueryContext(ctx, getMonthSummaryByProject)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetMonthSummaryByProjectRow
	for rows.Next() {
		var i GetMonthSummaryByProjectRow
		if err := rows.Scan(
			&i.ProjectID,
			&i.ProjectName,
			&i.ClientID,
			&i.ClientName,
			&i.TotalSeconds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getMostRecentTimeEntry = `-- name: GetMostRecentTimeEntry :one
select id, start_time, end_time, description, client_id, project_id, billable_rate from time_entry 
order by start_time desc
limit 1
`

func (q *Queries) GetMostRecentTimeEntry(ctx context.Context) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, getMostRecentTimeEntry)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const getProjectByNameAndClient = `-- name: GetProjectByNameAndClient :one
select id, name, client_id, billable_rate, archived, created_at from project where name = ?1 and client_id = ?2 limit 1
`

type GetProjectByNameAndClientParams struct {
	Name     string
	ClientID int64
}

func (q *Queries) GetProjectByNameAndClient(ctx context.Context, arg GetProjectByNameAndClientParams) (Project, error) {
	row := q.db.QueryRowContext(ctx, getProjectByNameAndClient, arg.Name, arg.ClientID)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.ClientID,
		&i.BillableRate,
		&i.Archived,
		&i.CreatedAt,
	)
	return i, err
}

const getTimeEntryById = `-- name: GetTimeEntryById :one
select id, start_time, end_time, description, client_id, project_id, billable_rate from time_entry
where id = ?1
`

func (q *Queries) GetTimeEntryById(ctx context.Context, entryID int64) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, getTimeEntryById, entryID)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const getTimesheetDataByClient = `-- name: GetTimesheetDataByClient :many
select 
    te.id as time_entry_id,
    te.start_time,
    te.end_time,
    te.description,
    te.billable_rate as entry_billable_rate,
    c.id as client_id,
    c.name as client_name,
    c.billable_rate as client_billable_rate,
    p.id as project_id,
    p.name as project_name,
    p.billable_rate as project_billable_rate,
    cast(round((julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60) as integer) as duration_seconds
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where c.id = ?1
    and te.start_time >= ?2
    and te.start_time <= ?3
    and te.end_time is not null
order by te.start_time
`

type GetTimesheetDataByClientParams struct {
	ClientID  int64
	StartTime time.Time
	EndTime   time.Time
}

type GetTimesheetDataByClientRow struct {
	TimeEntryID         int64
	StartTime           time.Time
	EndTime             sql.NullTime
	Description         sql.NullString
	EntryBillableRate   sql.NullInt64
	ClientID            int64
	ClientName          string
	ClientBillableRate  sql.NullInt64
	ProjectID           sql.NullInt64
	ProjectName         sql.NullString
	ProjectBillableRate sql.NullInt64
	DurationSeconds     int64
}

func (q *Queries) GetTimesheetDataByClient(ctx context.Context, arg GetTimesheetDataByClientParams) ([]GetTimesheetDataByClientRow, error) {
	rows, err := q.db.QueryContext(ctx, getTimesheetDataByClient, arg.ClientID, arg.StartTime, arg.EndTime)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetTimesheetDataByClientRow
	for rows.Next() {
		var i GetTimesheetDataByClientRow
		if err := rows.Scan(
			&i.TimeEntryID,
			&i.StartTime,
			&i.EndTime,
			&i.Description,
			&i.EntryBillableRate,
			&i.ClientID,
			&i.ClientName,
			&i.ClientBillableRate,
			&i.ProjectID,
			&i.ProjectName,
			&i.ProjectBillableRate,
			&i.DurationSeconds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getTimesheetDataByProject = `-- name: GetTimesheetDataByProject :many
select 
    te.id as time_entry_id,
    te.start_time,
    te.end_time,
    te.description,
    te.billable_rate as entry_billable_rate,
    c.id as client_id,
    c.name as client_name,
    c.billable_rate as client_billable_rate,
    p.id as project_id,
    p.name as project_name,
    p.billable_rate as project_billable_rate,
    cast(
        case 
            when te.end_time is null then 
                (julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
            else 
                (julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
        end as integer
    ) as duration_seconds
from time_entry te
join client c on te.client_id = c.id
join project p on te.project_id = p.id
where p.id = ?1
    and te.start_time >= ?2
    and te.start_time <= ?3
    and te.end_time is not null
order by te.start_time
`

type GetTimesheetDataByProjectParams struct {
	ProjectID int64
	StartTime time.Time
	EndTime   time.Time
}

type GetTimesheetDataByProjectRow struct {
	TimeEntryID         int64
	StartTime           time.Time
	EndTime             sql.NullTime
	Description         sql.NullString
	EntryBillableRate   sql.NullInt64
	ClientID            int64
	ClientName          string
	ClientBillableRate  sql.NullInt64
	ProjectID           int64
	ProjectName         string
	ProjectBillableRate sql.NullInt64
	DurationSeconds     int64
}

func (q *Queries) GetTimesheetDataByProject(ctx context.Context, arg GetTimesheetDataByProjectParams) ([]GetTimesheetDataByProjectRow, error) {
	rows, err := q.db.QueryContext(ctx, getTimesheetDataByProject, arg.ProjectID, arg.StartTime, arg.EndTime)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetTimesheetDataByProjectRow
	for rows.Next() {
		var i GetTimesheetDataByProjectRow
		if err := rows.Scan(
			&i.TimeEntryID,
			&i.StartTime,
			&i.EndTime,
			&i.Description,
			&i.EntryBillableRate,
			&i.ClientID,
			&i.ClientName,
			&i.ClientBillableRate,
			&i.ProjectID,
			&i.ProjectName,
			&i.ProjectBillableRate,
			&i.DurationSeconds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getTodaySummary = `-- name: GetTodaySummary :one
select 
    cast(sum(
        case 
            when te.end_time is null then 
                (julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
            else 
                (julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
        end
    ) as integer) as total_seconds
from time_entry te
where date(te.start_time, 'localtime') = date('now', 'localtime')
`

func (q *Queries) GetTodaySummary(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, getTodaySummary)
	var total_seconds int64
	err := row.Scan(&total_seconds)
	return total_seconds, err
}

const getWeekSummaryByProject = `-- name: GetWeekSummaryByProject :many
select 
    p.id as project_id,
    p.name as project_name,
    c.id as client_id,
    c.name as client_name,
    cast(sum(
        case 
            when te.end_time is null then 
                (julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
            else 
                (julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
        end
    ) as integer) as total_seconds
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where date(te.start_time, 'localtime') >= date('now', 'localtime', 'weekday 0', '-6 days')
    and date(te.start_time) <= date('now', 'utc')
group by p.id, p.name, c.id, c.name
order by c.name, p.name
`

type GetWeekSummaryByProjectRow struct {
	ProjectID    sql.NullInt64
	ProjectName  sql.NullString
	ClientID     int64
	ClientName   string
	TotalSeconds int64
}

func (q *Queries) GetWeekSummaryByProject(ctx context.Context) ([]GetWeekSummaryByProjectRow, error) {
	rows, err := q.db.QueryContext(ctx, getWeekSummaryByProject)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetWeekSummaryByProjectRow
	for rows.Next() {
		var i GetWeekSummaryByProjectRow
		if err := rows.Scan(
			&i.ProjectID,
			&i.ProjectName,
			&i.ClientID,
			&i.ClientName,
			&i.TotalSeconds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listAllClients = `-- name: ListAllClients :many
select id, name, email, billable_rate, archived, created_at from client
order by name
`

func (q *Queries) ListAllClients(ctx context.Context) ([]Client, error) {
	rows, err := q.db.QueryContext(ctx, listAllClients)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Client
	for rows.Next() {
		var i Client
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Email,
			&i.BillableRate,
			&i.Archived,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listAllProjects = `-- name: ListAllProjects :many
select p.id, p.name, p.client_id, p.billable_rate, p.archived, p.created_at, c.name as client_name from project p
join client c on p.client_id = c.id
order by c.name, p.name
`

type ListAllProjectsRow struct {
	ID           int64
	Name         string
	ClientID     int64
	BillableRate sql.NullInt64
	Archived     int64
	CreatedAt    sql.NullTime
	ClientName   string
}

func (q *Queries) ListAllProjects(ctx context.Context) ([]ListAllProjectsRow, error) {
	rows, err := q.db.QueryContext(ctx, listAllProjects)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListAllProjectsRow
	for rows.Next() {
		var i ListAllProjectsRow
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.ClientID,
			&i.BillableRate,
			&i.Archived,
			&i.CreatedAt,
			&i.ClientName,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const removeTimeEntry = `-- name: RemoveTimeEntry :exec
delete from time_entry
where id = ?1
`

func (q *Queries) RemoveTimeEntry(ctx context.Context, entryID int64) error {
	_, err := q.db.ExecContext(ctx, removeTimeEntry, entryID)
	return err
}

const stopTimeEntry = `-- name: StopTimeEntry :one
update time_entry 
set end_time = datetime('now', 'utc')
where id = (
	select id
	from time_entry
	where end_time is null
	order by start_time desc
	limit 1
)
returning id, start_time, end_time, description, client_id, project_id, billable_rate
`

func (q *Queries) StopTimeEntry(ctx context.Context) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, stopTimeEntry)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const unarchiveClient = `-- name: UnarchiveClient :exec
update client
set archived = 0
where id = ?1
`

func (q *Queries) UnarchiveClient(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, unarchiveClient, id)
	return err
}

const unarchiveProject = `-- name: UnarchiveProject :exec
update project
set archived = 0
where id = ?1
`

func (q *Queries) UnarchiveProject(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, unarchiveProject, id)
	return err
}

const updateActiveTimerDescription = `-- name: UpdateActiveTimerDescription :exec
update time_entry 
set description = ?1
where id = (
	select id
	from time_entry
	where end_time is null
	order by start_time desc
	limit 1
)
`

func (q *Queries) UpdateActiveTimerDescription(ctx context.Context, description sql.NullString) error {
	_, err := q.db.ExecContext(ctx, updateActiveTimerDescription, description)
	return err
}

const updateClient = `-- name: UpdateClient :one
update client
set name = ?1, email = ?2, billable_rate = ?3
where id = ?4
returning id, name, email, billable_rate, archived, created_at
`

type UpdateClientParams struct {
	Name         string
	Email        sql.NullString
	BillableRate sql.NullInt64
	ID           int64
}

func (q *Queries) UpdateClient(ctx context.Context, arg UpdateClientParams) (Client, error) {
	row := q.db.QueryRowContext(ctx, updateClient,
		arg.Name,
		arg.Email,
		arg.BillableRate,
		arg.ID,
	)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Email,
		&i.BillableRate,
		&i.Archived,
		&i.CreatedAt,
	)
	return i, err
}

const updateContractor = `-- name: UpdateContractor :one
update contractor 
set name = ?1, label = ?2, email = ?3
where id = (select id from contractor limit 1)
returning id, name, label, email, created_at
`

type UpdateContractorParams struct {
	Name  string
	Label string
	Email string
}

func (q *Queries) UpdateContractor(ctx context.Context, arg UpdateContractorParams) (Contractor, error) {
	row := q.db.QueryRowContext(ctx, updateContractor, arg.Name, arg.Label, arg.Email)
	var i Contractor
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Label,
		&i.Email,
		&i.CreatedAt,
	)
	return i, err
}

const updateProject = `-- name: UpdateProject :one
update project
set name = ?1, billable_rate = ?2
where id = ?3
returning id, name, client_id, billable_rate, archived, created_at
`

type UpdateProjectParams struct {
	Name         string
	BillableRate sql.NullInt64
	ID           int64
}

func (q *Queries) UpdateProject(ctx context.Context, arg UpdateProjectParams) (Project, error) {
	row := q.db.QueryRowContext(ctx, updateProject, arg.Name, arg.BillableRate, arg.ID)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.ClientID,
		&i.BillableRate,
		&i.Archived,
		&i.CreatedAt,
	)
	return i, err
}