
import logging
from aiogram import Router, F
from aiogram.types import CallbackQuery, InlineKeyboardMarkup, InlineKeyboardButton
from utils.helpers import check_membership_and_notify_callback
from db import db

logger = logging.getLogger(__name__)

router = Router()

@router.callback_query(F.data == "auth_gates")
async def auth_gates_menu(callback: CallbackQuery):
    try:
        if not await check_membership_and_notify_callback(callback):
            return
        
        auth1_status = await db.get_gate_status('auth1')
        auth2_status = await db.get_gate_status('auth2')
        vbv_status = await db.get_gate_status('vbv')
        stripeauth_status = await db.get_gate_status('stripeauth')
        
        auth1_text = "Auth1" if auth1_status else "Auth1 (bakımda...)"
        auth2_text = "Auth2" if auth2_status else "Auth2 (bakımda...)"
        vbv_text = "VBV API" if vbv_status else "VBV API (bakımda...)"
        stripeauth_text = "STRIPE AUTH" if stripeauth_status else "STRIPE AUTH (bakımda...)"
        
        keyboard = InlineKeyboardMarkup(inline_keyboard=[
            [InlineKeyboardButton(text=auth1_text, callback_data="auth1_info")],
            [InlineKeyboardButton(text=auth2_text, callback_data="auth2_info")],
            [InlineKeyboardButton(text=vbv_text, callback_data="vbv_api")],
            [InlineKeyboardButton(text=stripeauth_text, callback_data="stripe_auth")],
            [InlineKeyboardButton(text="BRAINTREE AUTH", callback_data="braintree_auth")],
            [InlineKeyboardButton(text="GO BACK", callback_data="go_back")]
        ])
        
        await callback.message.edit_text("🔒AUTH GATES\n\nSelect an authentication gateway:", reply_markup=keyboard)
        await callback.answer()
    except Exception as e:
        logger.error(f"Error in AUTH GATES menu: {e}")
        await callback.answer("An error occurred.")

@router.callback_query(F.data == "charge_gates")
async def charge_gates_menu(callback: CallbackQuery):
    try:
        if not await check_membership_and_notify_callback(callback):
            return
        
        disney_status = await db.get_gate_status('disney')
        authnet_status = await db.get_gate_status('authnet')
        shopify1_status = await db.get_gate_status('shopify1')
        
        disney_text = "DISNEY 4$ CHARGE" if disney_status else "DISNEY 4$ CHARGE (bakımda...)"
        authnet_text = "AUTHORIZE.NET 10$ CHARGE" if authnet_status else "AUTHORIZE.NET 10$ CHARGE (bakımda...)"
        shopify_text = "SHOPIFY API'S" if shopify1_status else "SHOPIFY API'S (bakımda...)"
        
        keyboard = InlineKeyboardMarkup(inline_keyboard=[
            [InlineKeyboardButton(text="2DMOKA 80₺ CHARGE", callback_data="2dmoka_charge")],
            [InlineKeyboardButton(text=disney_text, callback_data="disney_charge")],
            [InlineKeyboardButton(text="BRAİNTREE 3$ CHARGE", callback_data="braintree_charge")],
            [InlineKeyboardButton(text=authnet_text, callback_data="authnet_info")],
            [InlineKeyboardButton(text="İYZİCO API'S", callback_data="iyzico_apis")],
            [InlineKeyboardButton(text="STRIPE API'S", callback_data="stripe_apis")],
            [InlineKeyboardButton(text=shopify_text, callback_data="shopify_apis")],
            [InlineKeyboardButton(text="GO BACK", callback_data="go_back")]
        ])
        await callback.message.edit_text("⚡️CHARGE GATES\n\nSelect a charge gateway:", reply_markup=keyboard)
        await callback.answer()
    except Exception as e:
        logger.error(f"Error in CHARGE GATES menu: {e}")
        await callback.answer("An error occurred.")

@router.callback_query(F.data == "puan_gates")
async def puan_gates_menu(callback: CallbackQuery):
    try:
        if not await check_membership_and_notify_callback(callback):
            return
        
        puan1_status = await db.get_gate_status('puan1')
        puan2_status = await db.get_gate_status('puan2')
        
        puan1_text = "PUAN1 CHECK" if puan1_status else "PUAN1 CHECK (bakımda...)"
        puan2_text = "PUAN2 CHECK" if puan2_status else "PUAN2 CHECK (bakımda...)"
        
        keyboard = InlineKeyboardMarkup(inline_keyboard=[
            [InlineKeyboardButton(text=puan1_text, callback_data="puan1_info")],
            [InlineKeyboardButton(text=puan2_text, callback_data="puan2_info")],
            [InlineKeyboardButton(text="PUAN3 TUMPUANLAR", callback_data="puan3_info")],
            [InlineKeyboardButton(text="GO BACK", callback_data="go_back")]
        ])
        
        await callback.message.edit_text("🇹🇷PUAN GATES\n\nSelect a puan checker:", reply_markup=keyboard)
        await callback.answer()
    except Exception as e:
        logger.error(f"Error in 🇹🇷PUAN GATES menu: {e}")
        await callback.answer("An error occurred.")

# Sub-menus (Iyzico, Stripe APIs etc)
@router.callback_query(F.data == "iyzico_apis")
async def iyzico_apis_info(callback: CallbackQuery):
    try:
        if not await check_membership_and_notify_callback(callback): return
        
        iyzico1_status = await db.get_gate_status('iyzico1')
        iyzico2_status = await db.get_gate_status('iyzico2')
        iyzico3_status = await db.get_gate_status('iyzico3')
        
        iyzico1_text = "İYZİCO1 CHARGE" if iyzico1_status else "İYZİCO1 CHARGE (bakımda...)"
        iyzico2_text = "İYZİCO2 395₺ CHARGE" if iyzico2_status else "İYZİCO2 395₺ CHARGE (bakımda...)"
        iyzico3_text = "İYZİCO3 CHARGE" if iyzico3_status else "İYZİCO3 CHARGE (bakımda...)"
        
        keyboard = InlineKeyboardMarkup(inline_keyboard=[
            [InlineKeyboardButton(text=iyzico1_text, callback_data="iyzico1_info")],
            [InlineKeyboardButton(text=iyzico2_text, callback_data="iyzico2_info")],
            [InlineKeyboardButton(text=iyzico3_text, callback_data="iyzico3_info")],
            [InlineKeyboardButton(text="İYZİCO4 1020₺ CHARGE", callback_data="iyzico4_info")],
            [InlineKeyboardButton(text="GO BACK", callback_data="charge_gates")]
        ])
        await callback.message.edit_text("İYZİCO API'S\n\nSelect an iyzico checker:", reply_markup=keyboard)
        await callback.answer()
    except Exception as e:
        logger.error(f"Error in iyzico menu: {e}")
        await callback.answer("An error occurred.")

@router.callback_query(F.data == "stripe_apis")
async def stripe_apis_info(callback: CallbackQuery):
    try:
        if not await check_membership_and_notify_callback(callback): return
        
        stripe1_status = await db.get_gate_status('stripe1')
        stripe2_status = await db.get_gate_status('stripe2')
        stripe3_status = await db.get_gate_status('stripe3')
        
        stripe1_text = "STRIPE1 CHARGE 10$" if stripe1_status else "STRIPE1 CHARGE 10$ (bakımda...)"
        stripe2_text = "STRIPE2" if stripe2_status else "STRIPE2 (bakımda...)"
        stripe3_text = "STRIPE3" if stripe3_status else "STRIPE3 (bakımda...)"
        
        keyboard = InlineKeyboardMarkup(inline_keyboard=[
            [InlineKeyboardButton(text=stripe1_text, callback_data="stripe1_info")],
            [InlineKeyboardButton(text=stripe2_text, callback_data="stripe2_info")],
            [InlineKeyboardButton(text=stripe3_text, callback_data="stripe3_info")],
            [InlineKeyboardButton(text="GO BACK", callback_data="charge_gates")]
        ])
        await callback.message.edit_text("STRIPE API'S\n\nSelect an API:", reply_markup=keyboard)
        await callback.answer()
    except Exception as e:
        logger.error(f"Error in stripe menu: {e}")
        await callback.answer("An error occurred.")

@router.callback_query(F.data == "shopify_apis")
async def shopify_apis_info(callback: CallbackQuery):
    try:
        if not await check_membership_and_notify_callback(callback): return
        
        keyboard = InlineKeyboardMarkup(inline_keyboard=[
            [InlineKeyboardButton(text="SHOPIFY1 CHECK", callback_data="shopify1_info")],
            [InlineKeyboardButton(text="GO BACK", callback_data="charge_gates")]
        ])
        
        await callback.message.edit_text("SHOPIFY API'S\n\nSelect an API:", reply_markup=keyboard)
        await callback.answer()
    except Exception as e:
        logger.error(f"Error in shopify menu: {e}")
        await callback.answer("An error occurred.")
