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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
# database.py β€” MySQL Connection & CRUD Operations for Scraper and Forecast
import pandas as pd
import sqlalchemy
import bcrypt
from sqlalchemy import create_engine, text
from app.config import get_settings

settings = get_settings()

# Global Singleton DB Engine
engine = create_engine(
    settings.DATABASE_URL,
    pool_pre_ping=True,
    pool_recycle=3600
)

def get_db_engine():
    return engine

def get_engine():
    """Compatibility alias for forecast operations."""
    return engine

# ── READ OPERATIONS ───────────────────────────────────────────────────────────

def get_all_komoditas():
    """Ambil semua komoditas aktif."""
    return pd.read_sql(
        "SELECT id, nama, slug, unit, volatile, volatilitas_skor FROM komoditas WHERE is_active=1",
        engine
    )

def get_harga_harian(komoditas_id: int, days: int = 730):
    """Ambil data harga harian untuk satu komoditas (default 2 tahun terakhir)."""
    query = f"""
        SELECT hh.tanggal AS date, p.nama AS pasar_nama, hh.harga
        FROM harga_harian hh
        JOIN pasar p ON p.id = hh.pasar_id
        WHERE hh.komoditas_id = {komoditas_id}
          AND hh.tanggal >= DATE_SUB(CURDATE(), INTERVAL {days} DAY)
        ORDER BY hh.tanggal ASC
    """
    df_raw = pd.read_sql(query, engine)
    if df_raw.empty:
        return pd.DataFrame()
    # Pivot β†’ wide format
    df = df_raw.pivot_table(
        index='date', columns='pasar_nama', values='harga'
    ).reset_index()
    df.columns.name = None
    df['date'] = pd.to_datetime(df['date'])
    df = df.sort_values('date').reset_index(drop=True)
    return df

def get_pasar_list(komoditas_id: int):
    """Ambil daftar pasar yang punya data untuk komoditas ini."""
    return pd.read_sql(f"""
        SELECT DISTINCT p.id, p.nama
        FROM harga_harian hh
        JOIN pasar p ON p.id = hh.pasar_id
        WHERE hh.komoditas_id = {komoditas_id}
    """, engine)

# ── WRITE OPERATIONS ──────────────────────────────────────────────────────────

def save_model_ml(data: dict):
    """Upsert ke tabel model_ml."""
    with engine.begin() as conn:
        conn.execute(text("""
            INSERT INTO model_ml (
                komoditas_id, nama_model, versi, deskripsi, file_path,
                mape, rmse, mae, r2_score, confidence_level,
                stabilitas, status_validasi, catatan_validasi,
                tanggal_training, tanggal_evaluasi, is_active,
                created_at, updated_at
            ) VALUES (
                :komoditas_id, :nama_model, :versi, :deskripsi, :file_path,
                :mape, :rmse, :mae, :r2_score, :confidence_level,
                :stabilitas, :status_validasi, :catatan_validasi,
                :tanggal_training, :tanggal_evaluasi, 1, NOW(), NOW()
            )
            ON DUPLICATE KEY UPDATE
                nama_model       = VALUES(nama_model),
                versi            = VALUES(versi),
                deskripsi        = VALUES(deskripsi),
                file_path        = VALUES(file_path),
                mape             = VALUES(mape),
                rmse             = VALUES(rmse),
                mae              = VALUES(mae),
                r2_score         = VALUES(r2_score),
                confidence_level = VALUES(confidence_level),
                stabilitas       = VALUES(stabilitas),
                status_validasi  = VALUES(status_validasi),
                catatan_validasi = VALUES(catatan_validasi),
                tanggal_evaluasi = VALUES(tanggal_evaluasi),
                updated_at       = NOW()
        """), data)

    with engine.connect() as conn:
        return conn.execute(
            text("SELECT id FROM model_ml WHERE komoditas_id=:kid ORDER BY updated_at DESC LIMIT 1"),
            {'kid': data['komoditas_id']}
        ).scalar()

def save_hasil_prediksi(predictions: list, komoditas_id: int, pasar_id, model_id: int, meta: dict):
    """Hapus prediksi lama & insert 7 baris baru."""
    with engine.begin() as conn:
        conn.execute(text("""
            DELETE FROM hasil_prediksi
            WHERE komoditas_id = :kid AND pasar_id = :pid
              AND tanggal_target >= CURDATE()
        """), {'kid': komoditas_id, 'pid': pasar_id})

        for p in predictions:
            conn.execute(text("""
                INSERT INTO hasil_prediksi (
                    komoditas_id, pasar_id, model_id,
                    tanggal_prediksi, tanggal_target,
                    harga_prediksi, confidence_level,
                    model_name, mape, rmse, created_at
                ) VALUES (
                    :kid, :pid, :model_id,
                    CURDATE(), :tanggal_target,
                    :harga_prediksi, :confidence_level,
                    :model_name, :mape, :rmse, NOW()
                )
            """), {
                'kid'             : komoditas_id,
                'pid'             : pasar_id,
                'model_id'        : model_id,
                'tanggal_target'  : p['tanggal'],
                'harga_prediksi'  : p['harga_prediksi'],
                'confidence_level': meta['confidence_level'],
                'model_name'      : meta['nama_model'],
                'mape'            : meta['mape'],
                'rmse'            : meta['rmse'],
            })

def save_ringkasan_prediksi(data: dict, komoditas_id: int, model_id: int):
    """Upsert ringkasan prediksi."""
    with engine.begin() as conn:
        conn.execute(text("""
            INSERT INTO ringkasan_prediksi (
                komoditas_id, model_id,
                harga_min, harga_max, tren, confidence_level,
                status_analisis, deskripsi_status,
                tanggal_mulai, tanggal_akhir, created_at
            ) VALUES (
                :komoditas_id, :model_id,
                :harga_min, :harga_max, :tren, :confidence_level,
                :status_analisis, :deskripsi_status,
                :tanggal_mulai, :tanggal_akhir, NOW()
            )
            ON DUPLICATE KEY UPDATE
                harga_min        = VALUES(harga_min),
                harga_max        = VALUES(harga_max),
                tren             = VALUES(tren),
                confidence_level = VALUES(confidence_level),
                status_analisis  = VALUES(status_analisis),
                deskripsi_status = VALUES(deskripsi_status),
                tanggal_mulai    = VALUES(tanggal_mulai),
                tanggal_akhir    = VALUES(tanggal_akhir)
        """), {**data, 'komoditas_id': komoditas_id, 'model_id': model_id})

def save_insight_prediksi(insights: list, komoditas_id: int, model_id: int):
    """Hapus insight lama & insert baru."""
    with engine.begin() as conn:
        conn.execute(text(
            "DELETE FROM insight_prediksi WHERE komoditas_id = :kid"
        ), {'kid': komoditas_id})
        for ins in insights:
            conn.execute(text("""
                INSERT INTO insight_prediksi (
                    komoditas_id, model_id, konten, tipe, ikon,
                    urutan, is_active, created_at, updated_at
                ) VALUES (
                    :kid, :model_id, :konten, :tipe, :ikon,
                    :urutan, 1, NOW(), NOW()
                )
            """), {
                'kid'     : komoditas_id,
                'model_id': model_id,
                'konten'  : ins['konten'],
                'tipe'    : ins['tipe'],
                'ikon'    : ins['ikon'],
                'urutan'  : ins['urutan'],
            })

def update_komoditas_volatilitas(komoditas_id: int, volatile: int, skor: float):
    with engine.begin() as conn:
        conn.execute(text("""
            UPDATE komoditas SET volatile=:v, volatilitas_skor=:s, updated_at=NOW()
            WHERE id=:kid
        """), {'v': volatile, 's': skor, 'kid': komoditas_id})

def save_all_model_results(all_results: dict, best_meta: dict, komoditas_id: int, pasar_id: int):
    """Save ALL model results to model_ml (not just best). Best gets is_active=1."""
    best_name = best_meta.get('nama_model', '')
    with engine.begin() as conn:
        # Deactivate old models for this komoditas
        conn.execute(text(
            "UPDATE model_ml SET is_active=0 WHERE komoditas_id=:kid"
        ), {'kid': komoditas_id})
        # Insert each model result
        for model_name, metrics in all_results.items():
            is_best = 1 if model_name == best_name or f"{model_name}_Tuned" == best_name else 0
            
            # Hitung stabilitas sederhana untuk kolom ENUM agar tidak error
            mape_val = metrics.get('mape', 0)
            stabilitas_label = (
                'optimal' if mape_val < 2 else 
                'baik' if mape_val < 5 else 
                'cukup' if mape_val < 10 else 
                'perlu_retrain'
            )

            conn.execute(text("""
                INSERT INTO model_ml (
                    komoditas_id, nama_model, versi, deskripsi, file_path,
                    mape, rmse, mae, r2_score, confidence_level,
                    stabilitas, status_validasi, catatan_validasi,
                    tanggal_training, tanggal_evaluasi, is_active,
                    created_at, updated_at
                ) VALUES (
                    :komoditas_id, :nama_model, '1.0', :deskripsi, '',
                    :mape, :rmse, :mae, :r2_score, 0,
                    :stabilitas, 'terverifikasi', '',
                    CURDATE(), CURDATE(), :is_active, NOW(), NOW()
                )
            """), {
                'komoditas_id': komoditas_id,
                'nama_model': model_name,
                'deskripsi': f"Model {model_name} untuk komoditas ID {komoditas_id}",
                'mape': round(mape_val, 4),
                'rmse': round(metrics.get('rmse', 0), 4),
                'mae': round(metrics.get('mae', 0), 4),
                'r2_score': round(metrics.get('r2', 0), 4),
                'stabilitas': stabilitas_label,
                'is_active': is_best,
            })

# ── READ OPERATIONS FOR FRONTEND API ──────────────────────────────────────────

def get_prediksi_data(komoditas_id: int):
    """Ambil semua data yang dibutuhkan frontend untuk satu komoditas."""
    komoditas = pd.read_sql(f"""
        SELECT k.*, kk.nama as kategori_nama
        FROM komoditas k
        JOIN kategori_komoditas kk ON kk.id = k.kategori_id
        WHERE k.id = {komoditas_id}
    """, engine).to_dict('records')
    komoditas = komoditas[0] if komoditas else None

    harga_terkini = pd.read_sql(f"""
        SELECT hh.harga, hh.tanggal, p.nama as pasar_nama, p.id as pasar_id
        FROM harga_harian hh
        JOIN pasar p ON p.id = hh.pasar_id
        WHERE hh.komoditas_id = {komoditas_id}
        ORDER BY hh.tanggal DESC LIMIT 1
    """, engine).to_dict('records')
    harga_terkini = harga_terkini[0] if harga_terkini else None

    # Best (active) model
    model = pd.read_sql(f"""
        SELECT * FROM model_ml WHERE komoditas_id={komoditas_id} AND is_active=1
        ORDER BY updated_at DESC LIMIT 1
    """, engine).to_dict('records')
    model = model[0] if model else None

    # ALL models for comparison (all runs)
    all_models = pd.read_sql(f"""
        SELECT nama_model, mape, rmse, mae, r2_score, is_active,
               tanggal_training, created_at
        FROM model_ml WHERE komoditas_id={komoditas_id}
        ORDER BY created_at DESC, rmse ASC
    """, engine).to_dict('records')

    ringkasan = pd.read_sql(f"""
        SELECT * FROM ringkasan_prediksi WHERE komoditas_id={komoditas_id}
        ORDER BY created_at DESC LIMIT 1
    """, engine).to_dict('records')
    ringkasan = ringkasan[0] if ringkasan else None

    hasil = pd.read_sql(f"""
        SELECT tanggal_target, harga_prediksi, confidence_level
        FROM hasil_prediksi
        WHERE komoditas_id={komoditas_id} AND tanggal_target >= CURDATE()
        ORDER BY tanggal_target ASC
    """, engine).to_dict('records')

    insights = pd.read_sql(f"""
        SELECT * FROM insight_prediksi
        WHERE komoditas_id={komoditas_id} AND is_active=1
        ORDER BY urutan ASC
    """, engine).to_dict('records')

    return {
        'komoditas'     : komoditas,
        'harga_terkini' : harga_terkini,
        'model'         : model,
        'all_models'    : all_models,
        'ringkasan'     : ringkasan,
        'prediksi_7hari': hasil,
        'insights'      : insights,
    }

def verify_user_mysql(email: str, password_input: str) -> dict:
    """
    Verify user credential using MySQL 'users' table.
    Checks email, bcrypt password hash (Laravel format), and validates super_admin/admin role.
    Auto-discovers actual column names from INFORMATION_SCHEMA to handle varying schemas.
    Returns user dict on success, raises Exception otherwise.
    """
    with engine.connect() as conn:
        # Step 1: Discover actual column names in the 'users' table
        try:
            col_query = text("""
                SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
                WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'users'
            """)
            columns = [row[0].lower() for row in conn.execute(col_query).fetchall()]
        except Exception as e:
            raise Exception(f"Gagal membaca skema tabel users: {str(e)}")

        if not columns:
            raise Exception("Tabel 'users' tidak ditemukan di database")

        # Step 2: Map logical fields to actual column names
        # Name column: could be 'name', 'nama', 'full_name', 'username'
        name_col = None
        for candidate in ['name', 'nama', 'full_name', 'username', 'nama_lengkap']:
            if candidate in columns:
                name_col = candidate
                break
        if not name_col:
            name_col = 'email'  # fallback to email as display name

        # Password column: could be 'password', 'kata_sandi', 'sandi', 'passwd'
        password_col = None
        for candidate in ['password', 'password_hash', 'kata_sandi', 'sandi', 'passwd', 'pass']:
            if candidate in columns:
                password_col = candidate
                break
        if not password_col:
            raise Exception(f"Kolom password tidak ditemukan di tabel users. Kolom yang ada: {', '.join(columns)}")

        # Role column: could be 'role', 'roles', 'user_role', 'level', 'tipe'
        role_col = None
        for candidate in ['role', 'roles', 'user_role', 'level', 'tipe', 'type']:
            if candidate in columns:
                role_col = candidate
                break
        if not role_col:
            raise Exception(f"Kolom role tidak ditemukan di tabel users. Kolom yang ada: {', '.join(columns)}")

        # Step 3: Build and execute the query
        try:
            sql = f"SELECT id, `{name_col}` as name, email, `{password_col}` as password, `{role_col}` as role FROM users WHERE email = :email LIMIT 1"
            res = conn.execute(text(sql), {"email": email}).fetchone()
        except Exception as e:
            raise Exception(f"Database error querying users table: {str(e)}")

        if not res:
            raise Exception("Email tidak terdaftar")

        user_data = dict(res._mapping)
        hashed_password = user_data.get("password")
        if not hashed_password:
            raise Exception("Password hash tidak ditemukan di database")

        # Step 4: Verify password using bcrypt
        try:
            # Laravel uses $2y$ prefix, python bcrypt expects $2b$ or $2a$
            compat_hash = hashed_password
            if hashed_password.startswith("$2y$"):
                compat_hash = "$2b$" + hashed_password[4:]
            
            is_valid = bcrypt.checkpw(
                password_input.encode('utf-8'),
                compat_hash.encode('utf-8')
            )
        except Exception as hash_err:
            raise Exception(f"Gagal memverifikasi password hash: {str(hash_err)}")

        if not is_valid:
            raise Exception("Password salah")

        # Step 5: Check role - must be admin or super_admin
        role = str(user_data.get("role", "")).lower().strip()
        if role not in ["admin", "super_admin", "superadmin", "super-admin"]:
            raise Exception(f"Akses ditolak: Role '{role}' tidak memiliki izin administrator")

        return {
            "id": user_data.get("id"),
            "name": user_data.get("name"),
            "email": user_data.get("email"),
            "role": user_data.get("role")
        }

def get_active_models_summary() -> dict:
    """
    Get summary of all active models and commodity model list for AI Center page.
    """
    with engine.connect() as conn:
        try:
            query = text("""
                SELECT k.id as komoditas_id, k.nama as komoditas_nama, k.unit, k.volatile,
                       m.nama_model, m.mape, m.rmse, m.mae, m.r2_score, m.status_validasi,
                       m.tanggal_training, m.is_active
                FROM komoditas k
                LEFT JOIN model_ml m ON m.komoditas_id = k.id AND m.is_active = 1
                WHERE k.is_active = 1
                ORDER BY k.nama ASC
            """)
            rows = conn.execute(query).fetchall()
        except Exception as e:
            raise Exception(f"Database error querying active models: {str(e)}")

        results = []
        for r in rows:
            d = dict(r._mapping)
            # handle date serialization nicely
            if d.get("tanggal_training"):
                d["tanggal_training"] = str(d["tanggal_training"])
            results.append(d)
        
        # Compute averages
        active_mapes = [r['mape'] for r in results if r['mape'] is not None]
        avg_mape = round(sum(active_mapes) / len(active_mapes), 2) if active_mapes else 0.0
        active_count = sum(1 for r in results if r['is_active'] == 1)
        
        latest_date = None
        for r in results:
            if r['tanggal_training']:
                dt_str = str(r['tanggal_training'])
                if not latest_date or dt_str > latest_date:
                    latest_date = dt_str
                    
        return {
            "total_active_models": active_count,
            "average_mape": avg_mape,
            "latest_inference": latest_date,
            "commodity_models": results
        }