#!/usr/bin/env python3
"""Validate CSV files: headers, numeric fields, duplicates."""
import csv, os, sys
BASE = os.path.dirname(os.path.dirname(__file__))
DATA = os.path.join(BASE,'data')
LOG_CSV = os.path.join(DATA,'dashboard_food_log.csv')
DAILY_CSV = os.path.join(DATA,'dashboard_daily_summary.csv')

def check_log():
    req=['event_id','row_id','date','time','source','raw_text','meal_type','food_name','quantity','unit','calories','protein_g','carbs_g','fat_g','confidence','processing_status','notes']
    if not os.path.exists(LOG_CSV): return False,'missing log'
    with open(LOG_CSV,encoding='utf-8') as f:
        r=csv.DictReader(f)
        if r.fieldnames!=req: return False,'bad headers in log'
        rows=list(r)
        ids=[row['row_id'] for row in rows]
        if len(ids)!=len(set(ids)): return False,'duplicate row_id'
    return True,'ok'

def check_daily():
    req=['date','cal','protein','steps','burned','creatine','supplements','last_updated']
    if not os.path.exists(DAILY_CSV): return False,'missing daily'
    with open(DAILY_CSV,encoding='utf-8') as f:
        r=csv.DictReader(f)
        if r.fieldnames!=req: return False,'bad headers in daily'
        rows=list(r)
        dates=[row['date'] for row in rows]
        if len(dates)!=len(set(dates)): return False,'duplicate dates'
        for row in rows:
            try:
                int(row['cal'])
                float(row['protein'])
            except Exception:
                return False,'non-numeric cal/protein'
            # required defaults must be present
            if row.get('steps','')=='' or row.get('burned','')=='' or row.get('creatine','')=='' or row.get('supplements','')=='':
                return False,'missing required daily defaults'
    return True,'ok'

def main():
    a,b=check_log();
    c,d=check_daily();
    ok = a and c
    print('LOG:',b)
    print('DAILY:',d)
    print('RESULT:', 'PASS' if ok else 'FAIL')
    return 0 if ok else 2

if __name__=='__main__':
    sys.exit(main())
