File size: 9,359 Bytes
5c98911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Hand-written types matching wedge schema.
// Replace with `supabase gen types typescript` after linking project.

export interface Profile {
  id: string
  display_name: string
  email: string | null
  region: string
  role: 'citizen' | 'admin'
  created_at: string
  updated_at: string
}

export interface Submission {
  id: string
  user_id: string
  image_url: string
  image_sha256: string
  ocr_result: OcrResult | null
  status: 'pending' | 'approved' | 'rejected' | 'duplicate'
  reject_reason: string | null
  reviewed_by: string | null
  reviewed_at: string | null
  campaign_id: string | null
  mission_id: string | null
  created_at: string
}

export interface OcrResult {
  raw_text: string
  cafe_name: string | null
  receipt_date: string | null
  has_tumbler_discount: boolean
  discount_amount: number | null
  confidence: number
}

export interface Activity {
  id: string
  submission_id: string
  user_id: string
  activity_type: string  // 'reusable_cup' (legacy) or mission proof_type
  cup_count: number
  co2_grams_saved: number
  cafe_name: string | null
  receipt_date: string | null
  mission_id: string | null
  verified_at: string
  is_koc_registered: boolean
}

export interface MyStats {
  user_id: string
  total_cups: number
  total_co2_grams: number
  total_co2_kg: number
  first_activity: string | null
  last_activity: string | null
}

export interface AdminTotals {
  total_citizens: number
  total_cups: number
  total_co2_grams: number
  total_co2_kg: number
  cups_this_week: number
  cups_this_month: number
}

export interface WeeklyActivity {
  week_start: string // ISO date, Monday
  cups: number
  co2_grams: number
  co2_kg: number
  active_citizens: number
}

export type CampaignStatus = 'draft' | 'pending_review' | 'active' | 'paused' | 'completed'
export type CampaignType = 'citizen_activity' | 'industrial_koc' | 'sme_support' | 'finance_trade'

export interface Campaign {
  id: string
  title: string
  description: string | null
  sponsor_id: string | null
  campaign_type?: CampaignType
  status: CampaignStatus
  start_date: string
  end_date: string | null
  budget_krw: number
  target_co2_kg: number
  target_participants: number
  methodology: string | null
  platform_fee_rate: number
  reward_pool_rate: number
  region: string
  nature_beneficiary_id: string | null
  nature_beneficiary_name: string | null
  created_at: string
  updated_at: string
}

export interface CampaignStats {
  campaign_id: string
  unique_participants: number
  total_submissions: number
  approved_count: number
  pending_count: number
  rejected_count: number
  total_co2_grams: number
  total_co2_kg: number
}

// --- Missions ---

export type MissionProofType = 'receipt' | 'photo' | 'gps' | 'combo' | 'supply_cert'
export type MissionVerification = 'ocr' | 'ai_photo' | 'gps_fence' | 'manual' | 'supplier_data'
export type MissionStatus = 'active' | 'paused' | 'completed'

export interface GpsConfig {
  lat: number
  lng: number
  radius_meters: number
}

export interface Mission {
  id: string
  campaign_id: string
  title: string
  description: string | null
  proof_type: MissionProofType
  verification_method: MissionVerification
  co2_per_action_grams: number
  daily_limit_per_user: number | null
  total_limit_per_user: number | null
  sort_order: number
  status: MissionStatus
  gps_config: GpsConfig | null
  ai_config: unknown
  created_at: string
  updated_at: string
}

export interface CampaignParticipant {
  id: string
  campaign_id: string
  user_id: string
  join_method: 'self' | 'auto' | 'invite'
  status: 'active' | 'withdrawn' | 'banned'
  joined_at: string
  updated_at: string
}

export interface MyMission {
  user_id: string
  campaign_id: string
  joined_at: string
  campaign_title: string
  campaign_status: CampaignStatus
  start_date: string
  end_date: string | null
  mission_id: string
  mission_title: string
  mission_description: string | null
  proof_type: MissionProofType
  verification_method: MissionVerification
  co2_per_action_grams: number
  daily_limit_per_user: number | null
  total_limit_per_user: number | null
  mission_status: MissionStatus
  sort_order: number
  gps_config: GpsConfig | null
  total_completed: number
  completed_today: number
  is_daily_limit_reached: boolean
  my_co2_grams: number
}

// --- Phase 3: Approval Queue & Notifications ---

export type ApprovalType =
  | 'campaign_review'
  | 'sme_application'
  | 'sme_selection'
  | 'trade_request'
  | 'point_conversion'
  | 'finance_registration'

export type ApprovalStatus = 'pending' | 'approved' | 'rejected' | 'cancelled'

export interface DbApprovalItem {
  id: string
  type: ApprovalType
  status: ApprovalStatus
  requester_id: string | null
  requester_name: string
  title: string
  description: string | null
  payload: Record<string, unknown>
  reviewer_id: string | null
  reviewer_name: string | null
  reviewed_at: string | null
  reject_reason: string | null
  created_at: string
  updated_at: string
}

export type NotificationType = 'approval_result' | 'new_request' | 'status_change' | 'system'

export interface DbNotification {
  id: string
  user_id: string
  type: NotificationType
  title: string
  body: string
  link_to: string | null
  related_approval_id: string | null
  read: boolean
  created_at: string
}

export interface DbPointConversion {
  id: string
  user_id: string
  co2_kg_converted: number
  credit_ton_received: number
  conversion_rate: number
  approval_id: string | null
  created_at: string
}

// --- Phase 3: Finance Tables ---

export type FinanceOrgType = 'bank' | 'insurance' | 'securities' | 'individual' | 'public'
export type DbFinanceRegStatus = 'pending' | 'approved' | 'rejected'
export type DbTradeStatus = 'pending' | 'approved' | 'contracted' | 'rejected' | 'cancelled'

export interface DbFinanceRegistration {
  id: string
  user_id: string
  org_name: string
  org_type: FinanceOrgType
  contact_name: string
  contact_email: string
  contact_phone: string
  business_number: string | null
  purpose: string
  status: DbFinanceRegStatus
  submitted_at: string
  reviewed_at: string | null
  updated_at: string
}

export interface DbFinanceTrade {
  id: string
  user_id: string
  listing_id: string
  project_name: string
  tons: number
  price_per_ton: number
  total_krw: number
  buyer_org: string
  status: DbTradeStatus
  admin_note: string | null
  contract_url: string | null
  requested_at: string
  reviewed_at: string | null
  contracted_at: string | null
  approval_id: string | null
}

// --- Patent Gap: Settlement Ledger ---

export type DbTransactionCategory =
  | 'BUDGET_DEPOSIT'
  | 'FEE_DEDUCTION'
  | 'POINT_REWARD'
  | 'CREDIT_ISSUANCE'
  | 'CREDIT_DONATION'
  | 'CREDIT_RETIRE'

export type DbAssetType = 'KRW' | 'POINT' | 'TON'

export interface DbSettlementLedger {
  id: string
  transaction_id: string
  campaign_id: string | null
  sender_id: string
  sender_name: string
  receiver_id: string
  receiver_name: string
  category: DbTransactionCategory
  asset: DbAssetType
  amount: number
  idempotency_key: string
  previous_hash: string | null
  entry_hash: string
  description: string | null
  reference_data: Record<string, unknown> | null
  created_at: string
}

// --- Patent Gap: Point Balances & Transactions ---

export interface DbPointBalance {
  user_id: string
  campaign_id: string
  total_earned: number
  total_used: number
  available: number  // computed
  updated_at: string
}

export interface DbPointTransaction {
  id: string
  user_id: string
  campaign_id: string | null
  type: 'earn' | 'convert' | 'use' | 'refund'
  amount: number
  submission_id: string | null
  description: string | null
  created_at: string
}

// --- Patent Gap: Campaign Escrow ---

export interface DbCampaignEscrow {
  campaign_id: string
  deposited_krw: number
  fee_deducted: number
  reward_disbursed: number
  available_krw: number  // computed
  updated_at: string
}

export type DbDepositStatus = 'pending' | 'confirmed' | 'failed' | 'refunded'

// ํฌ์ธํŠธ ํŠธ๋žœ์žญ์…˜ ์œ ํ˜• (point_transactions.type CHECK ์™€ ๋™๊ธฐ)
export type PointTxType = 'earn' | 'convert' | 'use' | 'refund'

export interface DbEscrowDeposit {
  id: string
  campaign_id: string
  depositor_id: string | null
  amount_krw: number
  payment_method: string | null
  payment_ref: string | null
  status: DbDepositStatus
  confirmed_at: string | null
  created_at: string
}

// --- SME Allocation (Phase 4) ---

export interface DbSmeAllocationRule {
  id: string
  campaign_id: string
  sme_org_id: string
  sme_org_name: string
  allocation_pct: number
  is_scope3_eligible: boolean
  created_at: string
  updated_at: string
}

export interface DbSmeCreditDistribution {
  id: string
  campaign_id: string
  rule_id: string
  sme_org_id: string
  sme_org_name: string
  credit_ton: number
  allocation_pct: number
  is_scope3_eligible: boolean
  ledger_txn_id: string | null
  distributed_at: string
}

// --- Methodology Vectors (Phase 5) ---

export interface DbMethodologyVector {
  id: string
  methodology_code: string
  registry: string
  name_ko: string
  name_en: string | null
  applicability: string
  emission_factors: Record<string, unknown> | null
  required_params: string[]
  embedding: number[]  // vector(1536) โ€” not directly used in TS
  created_at: string
  updated_at: string
}