#!/usr/bin/env python3
"""
Data Compilation Script: Indian Vegetarian Foods Nutrition Database

This script creates a comprehensive structured dataset of common Indian vegetarian
foods from all major regions (North, South, East, West) with nutritional profiles.

Nutritional values are approximate per 100g or standard serving, based on:
- ICMR-NIN (Indian Council of Medical Research - National Institute of Nutrition)
- Standard nutritional databases for Indian foods
"""

import pandas as pd
import numpy as np
from pathlib import Path

# Set random seed for reproducibility
np.random.seed(42)

def create_indian_food_database():
    """
    Create a comprehensive database of Indian vegetarian foods with nutritional data.

    Returns:
        pd.DataFrame: Structured dataset with nutritional information
    """

    # Define comprehensive dataset with diverse Indian vegetarian foods
    foods_data = [
        # NORTH INDIAN - Breakfast
        {"name": "Paratha (Plain)", "region": "North", "category": "Breakfast",
         "calories": 300, "protein": 6.5, "carbohydrates": 42, "fats": 12, "fiber": 3.5},
        {"name": "Aloo Paratha", "region": "North", "category": "Breakfast",
         "calories": 330, "protein": 7.0, "carbohydrates": 48, "fats": 13, "fiber": 4.0},
        {"name": "Poha (Flattened Rice)", "region": "North", "category": "Breakfast",
         "calories": 180, "protein": 3.5, "carbohydrates": 38, "fats": 2.5, "fiber": 2.0},
        {"name": "Upma (Semolina)", "region": "North", "category": "Breakfast",
         "calories": 200, "protein": 4.5, "carbohydrates": 36, "fats": 4.5, "fiber": 2.5},

        # NORTH INDIAN - Lunch/Dinner
        {"name": "Dal Makhani", "region": "North", "category": "Lunch",
         "calories": 220, "protein": 9.0, "carbohydrates": 25, "fats": 9.5, "fiber": 6.0},
        {"name": "Chole (Chickpea Curry)", "region": "North", "category": "Lunch",
         "calories": 240, "protein": 11.0, "carbohydrates": 32, "fats": 7.0, "fiber": 8.5},
        {"name": "Rajma (Kidney Bean Curry)", "region": "North", "category": "Lunch",
         "calories": 230, "protein": 10.5, "carbohydrates": 30, "fats": 6.5, "fiber": 9.0},
        {"name": "Paneer Butter Masala", "region": "North", "category": "Dinner",
         "calories": 280, "protein": 14.0, "carbohydrates": 12, "fats": 20, "fiber": 2.5},
        {"name": "Palak Paneer", "region": "North", "category": "Dinner",
         "calories": 240, "protein": 13.5, "carbohydrates": 10, "fats": 17, "fiber": 3.5},
        {"name": "Aloo Gobi", "region": "North", "category": "Dinner",
         "calories": 150, "protein": 3.5, "carbohydrates": 22, "fats": 5.5, "fiber": 4.5},

        # NORTH INDIAN - Snacks
        {"name": "Samosa (2 pieces)", "region": "North", "category": "Snack",
         "calories": 260, "protein": 5.0, "carbohydrates": 36, "fats": 11, "fiber": 3.0},
        {"name": "Pakora (Mixed Veg)", "region": "North", "category": "Snack",
         "calories": 220, "protein": 4.5, "carbohydrates": 28, "fats": 10, "fiber": 3.5},

        # SOUTH INDIAN - Breakfast
        {"name": "Idli (3 pieces)", "region": "South", "category": "Breakfast",
         "calories": 156, "protein": 4.8, "carbohydrates": 30, "fats": 1.2, "fiber": 2.0},
        {"name": "Dosa (Plain)", "region": "South", "category": "Breakfast",
         "calories": 168, "protein": 4.5, "carbohydrates": 32, "fats": 2.0, "fiber": 2.5},
        {"name": "Masala Dosa", "region": "South", "category": "Breakfast",
         "calories": 250, "protein": 6.0, "carbohydrates": 42, "fats": 6.5, "fiber": 4.0},
        {"name": "Vada (2 pieces)", "region": "South", "category": "Breakfast",
         "calories": 240, "protein": 6.5, "carbohydrates": 28, "fats": 11, "fiber": 3.5},
        {"name": "Uttapam", "region": "South", "category": "Breakfast",
         "calories": 190, "protein": 5.5, "carbohydrates": 35, "fats": 3.0, "fiber": 3.0},

        # SOUTH INDIAN - Lunch/Dinner
        {"name": "Sambar", "region": "South", "category": "Lunch",
         "calories": 120, "protein": 5.5, "carbohydrates": 18, "fats": 3.0, "fiber": 5.0},
        {"name": "Rasam", "region": "South", "category": "Lunch",
         "calories": 80, "protein": 2.5, "carbohydrates": 12, "fats": 2.5, "fiber": 2.0},
        {"name": "Avial (Mixed Veg Curry)", "region": "South", "category": "Dinner",
         "calories": 180, "protein": 4.0, "carbohydrates": 20, "fats": 9.0, "fiber": 6.0},
        {"name": "Pongal (Savory)", "region": "South", "category": "Breakfast",
         "calories": 210, "protein": 6.0, "carbohydrates": 35, "fats": 5.5, "fiber": 2.5},
        {"name": "Kootu (Lentil Vegetable)", "region": "South", "category": "Lunch",
         "calories": 140, "protein": 6.5, "carbohydrates": 22, "fats": 3.5, "fiber": 5.5},

        # SOUTH INDIAN - Snacks
        {"name": "Murukku", "region": "South", "category": "Snack",
         "calories": 480, "protein": 8.0, "carbohydrates": 58, "fats": 23, "fiber": 2.5},
        {"name": "Bondas (2 pieces)", "region": "South", "category": "Snack",
         "calories": 200, "protein": 4.0, "carbohydrates": 26, "fats": 9.0, "fiber": 3.0},

        # EAST INDIAN - Breakfast/Lunch
        {"name": "Luchi (Puri)", "region": "East", "category": "Breakfast",
         "calories": 340, "protein": 6.0, "carbohydrates": 45, "fats": 15, "fiber": 2.5},
        {"name": "Aloo Dum", "region": "East", "category": "Lunch",
         "calories": 210, "protein": 3.5, "carbohydrates": 28, "fats": 9.5, "fiber": 4.0},
        {"name": "Cholar Dal", "region": "East", "category": "Lunch",
         "calories": 190, "protein": 8.5, "carbohydrates": 26, "fats": 5.5, "fiber": 7.0},
        {"name": "Shukto (Mixed Veg)", "region": "East", "category": "Lunch",
         "calories": 150, "protein": 4.5, "carbohydrates": 18, "fats": 7.0, "fiber": 5.5},

        # EAST INDIAN - Dinner/Snacks
        {"name": "Dhokar Dalna", "region": "East", "category": "Dinner",
         "calories": 240, "protein": 10.0, "carbohydrates": 22, "fats": 12, "fiber": 4.5},
        {"name": "Phulka/Roti (2 pieces)", "region": "East", "category": "Dinner",
         "calories": 150, "protein": 5.0, "carbohydrates": 32, "fats": 0.8, "fiber": 3.5},
        {"name": "Ghugni (Dried Peas)", "region": "East", "category": "Snack",
         "calories": 180, "protein": 8.0, "carbohydrates": 28, "fats": 3.5, "fiber": 8.0},
        {"name": "Nimki (Savory Crackers)", "region": "East", "category": "Snack",
         "calories": 460, "protein": 7.5, "carbohydrates": 55, "fats": 22, "fiber": 2.0},

        # WEST INDIAN - Breakfast/Lunch
        {"name": "Dhokla", "region": "West", "category": "Breakfast",
         "calories": 160, "protein": 5.5, "carbohydrates": 28, "fats": 3.0, "fiber": 2.5},
        {"name": "Thepla", "region": "West", "category": "Breakfast",
         "calories": 280, "protein": 6.5, "carbohydrates": 40, "fats": 10, "fiber": 4.5},
        {"name": "Khandvi", "region": "West", "category": "Snack",
         "calories": 180, "protein": 6.0, "carbohydrates": 24, "fats": 7.0, "fiber": 2.0},
        {"name": "Undhiyu", "region": "West", "category": "Lunch",
         "calories": 220, "protein": 6.5, "carbohydrates": 30, "fats": 8.5, "fiber": 7.5},
        {"name": "Dal Dhokli", "region": "West", "category": "Lunch",
         "calories": 200, "protein": 7.5, "carbohydrates": 32, "fats": 4.5, "fiber": 5.0},

        # WEST INDIAN - Dinner/Snacks
        {"name": "Pav Bhaji", "region": "West", "category": "Dinner",
         "calories": 280, "protein": 6.0, "carbohydrates": 42, "fats": 10, "fiber": 5.5},
        {"name": "Bharli Vangi (Stuffed Eggplant)", "region": "West", "category": "Dinner",
         "calories": 190, "protein": 4.5, "carbohydrates": 20, "fats": 10, "fiber": 6.0},
        {"name": "Fafda", "region": "West", "category": "Snack",
         "calories": 440, "protein": 8.5, "carbohydrates": 50, "fats": 22, "fiber": 3.0},
        {"name": "Sev", "region": "West", "category": "Snack",
         "calories": 520, "protein": 10.0, "carbohydrates": 48, "fats": 32, "fiber": 3.5},

        # DAIRY & ACCOMPANIMENTS (Pan-Indian)
        {"name": "Plain Yogurt (Dahi)", "region": "North", "category": "Dairy/Accompaniments",
         "calories": 60, "protein": 3.5, "carbohydrates": 4.5, "fats": 3.3, "fiber": 0},
        {"name": "Paneer (Cottage Cheese)", "region": "North", "category": "Dairy/Accompaniments",
         "calories": 265, "protein": 18.3, "carbohydrates": 1.2, "fats": 20.8, "fiber": 0},
        {"name": "Lassi (Sweet)", "region": "North", "category": "Dairy/Accompaniments",
         "calories": 110, "protein": 3.0, "carbohydrates": 18, "fats": 3.0, "fiber": 0},
        {"name": "Raita (Cucumber)", "region": "North", "category": "Dairy/Accompaniments",
         "calories": 55, "protein": 2.5, "carbohydrates": 6.0, "fats": 2.5, "fiber": 0.5},
        {"name": "Coconut Chutney", "region": "South", "category": "Dairy/Accompaniments",
         "calories": 160, "protein": 2.0, "carbohydrates": 8.0, "fats": 14, "fiber": 3.0},
        {"name": "Pickle (Mixed)", "region": "North", "category": "Dairy/Accompaniments",
         "calories": 145, "protein": 2.0, "carbohydrates": 12, "fats": 10, "fiber": 4.0},
        {"name": "Papad (Roasted)", "region": "South", "category": "Dairy/Accompaniments",
         "calories": 315, "protein": 12.0, "carbohydrates": 52, "fats": 3.5, "fiber": 8.0},
        {"name": "Rice (Cooked White)", "region": "South", "category": "Lunch",
         "calories": 130, "protein": 2.7, "carbohydrates": 28, "fats": 0.3, "fiber": 0.4},
        {"name": "Chapati (Whole Wheat)", "region": "North", "category": "Dinner",
         "calories": 120, "protein": 3.5, "carbohydrates": 24, "fats": 1.5, "fiber": 3.0},
    ]

    # Convert to DataFrame
    df = pd.DataFrame(foods_data)

    return df


def validate_dataset(df):
    """
    Perform validation checks on the dataset.

    Args:
        df: pandas DataFrame to validate

    Returns:
        tuple: (is_valid, validation_report)
    """
    validation_report = []
    is_valid = True

    # Check 1: Minimum number of items
    n_items = len(df)
    validation_report.append(f"✓ Total items: {n_items} (minimum 40 required)")
    if n_items < 40:
        is_valid = False
        validation_report.append("✗ ERROR: Dataset has fewer than 40 items")

    # Check 2: Check for null values
    null_counts = df.isnull().sum()
    if null_counts.any():
        is_valid = False
        validation_report.append("✗ ERROR: Null values found:")
        for col, count in null_counts[null_counts > 0].items():
            validation_report.append(f"  - {col}: {count} nulls")
    else:
        validation_report.append("✓ No null values detected")

    # Check 3: Ensure all required columns exist
    required_columns = ['name', 'region', 'category', 'calories', 'protein',
                       'carbohydrates', 'fats', 'fiber']
    missing_cols = set(required_columns) - set(df.columns)
    if missing_cols:
        is_valid = False
        validation_report.append(f"✗ ERROR: Missing columns: {missing_cols}")
    else:
        validation_report.append(f"✓ All required columns present: {required_columns}")

    # Check 4: Verify numeric types for nutritional columns
    numeric_cols = ['calories', 'protein', 'carbohydrates', 'fats', 'fiber']
    for col in numeric_cols:
        if col in df.columns:
            if not pd.api.types.is_numeric_dtype(df[col]):
                is_valid = False
                validation_report.append(f"✗ ERROR: Column '{col}' is not numeric (type: {df[col].dtype})")
            else:
                validation_report.append(f"✓ Column '{col}' is numeric")

    # Check 5: Regional representation
    regions = df['region'].unique()
    required_regions = {'North', 'South', 'East', 'West'}
    missing_regions = required_regions - set(regions)
    if missing_regions:
        is_valid = False
        validation_report.append(f"✗ ERROR: Missing regions: {missing_regions}")
    else:
        validation_report.append(f"✓ All 4 regions represented: {sorted(regions)}")

    # Regional distribution
    region_counts = df['region'].value_counts()
    validation_report.append("\nRegional Distribution:")
    for region, count in region_counts.items():
        validation_report.append(f"  - {region}: {count} items")

    # Check 6: Category representation
    categories = df['category'].unique()
    validation_report.append(f"\n✓ Categories present: {sorted(categories)}")
    category_counts = df['category'].value_counts()
    validation_report.append("\nCategory Distribution:")
    for cat, count in category_counts.items():
        validation_report.append(f"  - {cat}: {count} items")

    # Check 7: Basic nutritional value ranges (sanity checks)
    validation_report.append("\nNutritional Value Ranges:")
    for col in numeric_cols:
        if col in df.columns:
            min_val = df[col].min()
            max_val = df[col].max()
            mean_val = df[col].mean()
            validation_report.append(f"  - {col}: min={min_val:.1f}, max={max_val:.1f}, mean={mean_val:.1f}")

    return is_valid, validation_report


def main():
    """Main execution function."""

    print("=" * 70)
    print("Indian Vegetarian Foods - Data Compilation & Standardization")
    print("=" * 70)
    print()

    # Step 1: Create the dataset
    print("Step 1: Creating Indian vegetarian foods database...")
    df = create_indian_food_database()
    print(f"✓ Created dataset with {len(df)} food items")
    print()

    # Step 2: Display sample data
    print("Step 2: Sample data preview:")
    print(df.head(10).to_string(index=False))
    print("...")
    print()

    # Step 3: Validate the dataset
    print("Step 3: Validating dataset...")
    is_valid, validation_report = validate_dataset(df)
    print("\n".join(validation_report))
    print()

    if not is_valid:
        print("✗ VALIDATION FAILED - Dataset does not meet requirements")
        return False

    print("✓ VALIDATION PASSED - Dataset meets all requirements")
    print()

    # Step 4: Save to CSV
    output_path = Path("/app/sandbox/session_20251221_093619_3390c5c3bed6/indian_food_nutrition.csv")
    print(f"Step 4: Saving dataset to {output_path.name}...")
    df.to_csv(output_path, index=False)
    print(f"✓ Dataset saved successfully")
    print()

    # Final summary
    print("=" * 70)
    print("SUMMARY")
    print("=" * 70)
    print(f"Total Food Items: {len(df)}")
    print(f"Regions Covered: {', '.join(sorted(df['region'].unique()))}")
    print(f"Categories: {', '.join(sorted(df['category'].unique()))}")
    print(f"Output File: {output_path}")
    print(f"File Size: {output_path.stat().st_size / 1024:.2f} KB")
    print("=" * 70)

    return True


if __name__ == "__main__":
    success = main()
    exit(0 if success else 1)
