import os
from dotenv import load_dotenv

load_dotenv(os.path.join(os.path.dirname(__file__), '..', '.env'))

DB_HOST     = os.getenv('DB_HOST', 'localhost')
DB_PORT     = int(os.getenv('DB_PORT', 3306))
DB_USER     = os.getenv('DB_USER', 'root')
DB_PASSWORD = os.getenv('DB_PASSWORD', '')
DB_NAME     = os.getenv('DB_NAME', 'fuel_station')
SECRET_KEY  = os.getenv('SECRET_KEY', 'CHANGE_ME_IN_PRODUCTION')
ALGORITHM   = 'HS256'
TOKEN_EXPIRE_HOURS = int(os.getenv('TOKEN_EXPIRE_HOURS', 10))

from urllib.parse import quote_plus
DATABASE_URL = (
    f"mysql+pymysql://{DB_USER}:{quote_plus(DB_PASSWORD)}"
    f"@{DB_HOST}:{DB_PORT}/{DB_NAME}?charset=utf8mb4"
)

# Physical pump groupings (pump number → nozzle codes on that pump)
PHYSICAL_PUMPS: dict[int, dict] = {
    1: {'label': 'Pump 1', 'nozzles': ['LP1'],               'desc': 'Lanka Petrol (LP92)'},
    2: {'label': 'Pump 2', 'nozzles': ['LP2', 'EURO3'],      'desc': 'Lanka Petrol (LP92) & Euro 3'},
    3: {'label': 'Pump 3', 'nozzles': ['LAD1'],              'desc': 'Lanka Auto Diesel (LAD)'},
    4: {'label': 'Pump 4', 'nozzles': ['LAD2', 'LADXM'],     'desc': 'Lanka Auto Diesel (LAD) & Xtra Mile Diesel'},
}

ALL_NOZZLES = ['LP1', 'LP2', 'EURO3', 'LAD1', 'LAD2', 'LADXM']

# Default pump assignments per shift (can be overridden via system_settings shift.<TYPE>.pumps)
# EM: only Pump 2 (LP2/EURO3) + Pump 4 (LAD2/LADXM) run overnight
# All other shifts: all 4 pumps running
SHIFT_PUMP_MAP: dict[str, list[str]] = {
    'EM':  ['LP2', 'EURO3', 'LAD2', 'LADXM'],
    'DAY': ALL_NOZZLES,
    'MS':  ALL_NOZZLES,
    'MG':  ALL_NOZZLES,
    'ES':  ALL_NOZZLES,
}

SHIFT_LABELS: dict[str, str] = {
    'EM':  'Early Morning (10PM – 5:30AM)',
    'DAY': 'Day Shift',
    'MS':  'Morning Shift',
    'MG':  'MG Morning',
    'ES':  'Evening Shift',
}

# Operational periods (how management sees the day)
SHIFT_PERIODS: list[dict] = [
    {
        'id':       'EM',
        'label':    'Early Morning',
        'time':     '10PM – 5:30AM',
        'pumpers':  1,
        'shifts':   ['EM'],
        'note':     '1 person — all 4 pumps running',
    },
    {
        'id':       'DAY',
        'label':    'Day Shift',
        'time':     'Morning – Afternoon',
        'pumpers':  2,
        'shifts':   ['DAY', 'MS'],
        'note':     '2 people — all 4 pumps running',
    },
    {
        'id':       'MG',
        'label':    'MG Morning',
        'time':     'Morning',
        'pumpers':  1,
        'shifts':   ['MG'],
        'note':     '1 person — all 4 pumps running',
    },
    {
        'id':       'ES',
        'label':    'Evening',
        'time':     'Afternoon – 10PM',
        'pumpers':  1,
        'shifts':   ['ES'],
        'note':     '1 person — all 4 pumps running',
    },
]
