"""
Email Service for Daily Helper
Handles sending email reports and notifications
"""

from datetime import datetime, date
from typing import Optional, Dict, List
from sqlalchemy.orm import Session
from app.models.models import Trade, Simulation, Notification
import logging

logger = logging.getLogger(__name__)


class EmailTemplate:
    """Email template generator"""

    @staticmethod
    def daily_report_html(
        user_email: str,
        daily_pnl: float,
        win_rate: float,
        winning_trades: int,
        losing_trades: int,
        best_trade: float,
        worst_trade: float,
        trades_count: int,
        completion_rate: float,
        portfolio_value: float,
    ) -> str:
        """Generate HTML for daily report email"""

        pnl_color = "green" if daily_pnl >= 0 else "red"
        win_rate_color = "green" if win_rate >= 50 else "orange" if win_rate >= 40 else "red"

        html = f"""
        <!DOCTYPE html>
        <html>
        <head>
            <style>
                body {{ font-family: Arial, sans-serif; color: #333; }}
                .container {{ max-width: 600px; margin: 0 auto; padding: 20px; }}
                .header {{ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 5px; margin-bottom: 20px; }}
                .section {{ margin-bottom: 20px; padding: 15px; background: #f5f5f5; border-left: 4px solid #667eea; border-radius: 3px; }}
                .metric {{ display: inline-block; margin-right: 20px; margin-bottom: 10px; }}
                .metric-label {{ font-size: 12px; color: #666; }}
                .metric-value {{ font-size: 24px; font-weight: bold; color: #333; }}
                .positive {{ color: #22c55e; }}
                .negative {{ color: #ef4444; }}
                .neutral {{ color: #f59e0b; }}
                .footer {{ text-align: center; color: #999; font-size: 12px; margin-top: 30px; border-top: 1px solid #ddd; padding-top: 20px; }}
                table {{ width: 100%; border-collapse: collapse; margin-top: 10px; }}
                th, td {{ padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }}
                th {{ background: #667eea; color: white; }}
                .btn {{ display: inline-block; background: #667eea; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin-top: 10px; }}
            </style>
        </head>
        <body>
            <div class="container">
                <div class="header">
                    <h1>📊 Daily Trading Report</h1>
                    <p>{date.today().strftime('%A, %B %d, %Y')}</p>
                </div>

                <div class="section">
                    <h2>Performance Summary</h2>
                    <div class="metric">
                        <div class="metric-label">Daily P&L</div>
                        <div class="metric-value {pnl_color}">
                            ${daily_pnl:,.2f}
                        </div>
                    </div>
                    <div class="metric">
                        <div class="metric-label">Win Rate</div>
                        <div class="metric-value {win_rate_color}">
                            {win_rate:.1f}%
                        </div>
                    </div>
                    <div class="metric">
                        <div class="metric-label">Portfolio Value</div>
                        <div class="metric-value">
                            ${portfolio_value:,.2f}
                        </div>
                    </div>
                </div>

                <div class="section">
                    <h2>Trade Statistics</h2>
                    <table>
                        <tr>
                            <th>Metric</th>
                            <th>Value</th>
                        </tr>
                        <tr>
                            <td>Total Trades</td>
                            <td><strong>{trades_count}</strong></td>
                        </tr>
                        <tr>
                            <td>Winning Trades</td>
                            <td><span class="positive">✓ {winning_trades}</span></td>
                        </tr>
                        <tr>
                            <td>Losing Trades</td>
                            <td><span class="negative">✗ {losing_trades}</span></td>
                        </tr>
                        <tr>
                            <td>Best Trade</td>
                            <td><span class="positive">${best_trade:,.2f}</span></td>
                        </tr>
                        <tr>
                            <td>Worst Trade</td>
                            <td><span class="negative">${worst_trade:,.2f}</span></td>
                        </tr>
                        <tr>
                            <td>Daily Checklist</td>
                            <td><strong>{completion_rate:.0f}% Complete</strong></td>
                        </tr>
                    </table>
                </div>

                <div class="section">
                    <h2>Tomorrow's Preparation</h2>
                    <p>✓ Review today's trades and journal entries</p>
                    <p>✓ Update your trading plan for tomorrow</p>
                    <p>✓ Set price alerts for key levels</p>
                    <p>✓ Prepare your morning checklist</p>
                    <a href="http://localhost:3000" class="btn">Open Trading Dashboard</a>
                </div>

                <div class="footer">
                    <p>This is an automated report from your Gold Trading Simulator</p>
                    <p>Keep trading smart! 📈</p>
                </div>
            </div>
        </body>
        </html>
        """
        return html

    @staticmethod
    def weekly_report_html(
        user_email: str,
        weekly_pnl: float,
        weekly_trades: int,
        win_rate: float,
        best_day: str,
        worst_day: str,
        best_trade: float,
        largest_loss: float,
    ) -> str:
        """Generate HTML for weekly report email"""

        pnl_color = "green" if weekly_pnl >= 0 else "red"

        html = f"""
        <!DOCTYPE html>
        <html>
        <head>
            <style>
                body {{ font-family: Arial, sans-serif; color: #333; }}
                .container {{ max-width: 600px; margin: 0 auto; padding: 20px; }}
                .header {{ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 5px; margin-bottom: 20px; }}
                .section {{ margin-bottom: 20px; padding: 15px; background: #f5f5f5; border-left: 4px solid #667eea; border-radius: 3px; }}
                .metric {{ display: inline-block; margin-right: 20px; margin-bottom: 10px; }}
                .metric-label {{ font-size: 12px; color: #666; }}
                .metric-value {{ font-size: 24px; font-weight: bold; }}
                .positive {{ color: #22c55e; }}
                .negative {{ color: #ef4444; }}
                .footer {{ text-align: center; color: #999; font-size: 12px; margin-top: 30px; border-top: 1px solid #ddd; padding-top: 20px; }}
                table {{ width: 100%; border-collapse: collapse; margin-top: 10px; }}
                th, td {{ padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }}
                th {{ background: #667eea; color: white; }}
            </style>
        </head>
        <body>
            <div class="container">
                <div class="header">
                    <h1>📈 Weekly Trading Summary</h1>
                    <p>Week of {(date.today()).strftime('%B %d')}</p>
                </div>

                <div class="section">
                    <h2>Weekly Performance</h2>
                    <div class="metric">
                        <div class="metric-label">Weekly P&L</div>
                        <div class="metric-value {pnl_color}">
                            ${weekly_pnl:,.2f}
                        </div>
                    </div>
                    <div class="metric">
                        <div class="metric-label">Total Trades</div>
                        <div class="metric-value">
                            {weekly_trades}
                        </div>
                    </div>
                    <div class="metric">
                        <div class="metric-label">Win Rate</div>
                        <div class="metric-value">
                            {win_rate:.1f}%
                        </div>
                    </div>
                </div>

                <div class="section">
                    <h2>Key Insights</h2>
                    <table>
                        <tr>
                            <th>Metric</th>
                            <th>Value</th>
                        </tr>
                        <tr>
                            <td>Best Day</td>
                            <td><strong>{best_day}</strong></td>
                        </tr>
                        <tr>
                            <td>Worst Day</td>
                            <td><strong>{worst_day}</strong></td>
                        </tr>
                        <tr>
                            <td>Best Single Trade</td>
                            <td><span class="positive">${best_trade:,.2f}</span></td>
                        </tr>
                        <tr>
                            <td>Largest Loss</td>
                            <td><span class="negative">${largest_loss:,.2f}</span></td>
                        </tr>
                    </table>
                </div>

                <div class="section">
                    <h2>Action Items for Next Week</h2>
                    <p>1. Review your best performing setups</p>
                    <p>2. Analyze losing trades for patterns</p>
                    <p>3. Update your trading journal with insights</p>
                    <p>4. Adjust your trading plan if needed</p>
                </div>

                <div class="footer">
                    <p>Keep up the consistent trading! 💪</p>
                </div>
            </div>
        </body>
        </html>
        """
        return html


class EmailService:
    """Service for sending emails"""

    @staticmethod
    async def send_email(
        recipient_email: str,
        subject: str,
        html_content: str,
    ) -> bool:
        """
        Send email (stub for integration with actual email service)

        In production, integrate with:
        - SendGrid
        - Mailgun
        - AWS SES
        - SMTP server
        """
        try:
            # TODO: Implement actual email sending
            # For now, just log it
            logger.info(f"Email to {recipient_email}: {subject}")
            logger.debug(f"HTML content length: {len(html_content)}")

            # In production, replace this with actual email sending:
            # import smtplib
            # from email.mime.text import MIMEText
            # from email.mime.multipart import MIMEMultipart
            #
            # msg = MIMEMultipart('alternative')
            # msg['Subject'] = subject
            # msg['From'] = EMAIL_FROM
            # msg['To'] = recipient_email
            # msg.attach(MIMEText(html_content, 'html'))
            #
            # with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
            #     server.starttls()
            #     server.login(SMTP_USER, SMTP_PASSWORD)
            #     server.send_message(msg)

            return True
        except Exception as e:
            logger.error(f"Failed to send email to {recipient_email}: {str(e)}")
            return False

    @staticmethod
    async def send_daily_report(
        db: Session,
        user_email: str,
    ) -> bool:
        """Send daily trading report email"""
        try:
            # Get today's trades
            today = date.today()
            trades = db.query(Trade).filter(
                db.func.date(Trade.timestamp) == today
            ).all()

            # Calculate metrics
            daily_pnl = sum(trade.pnl or 0 for trade in trades)
            winning_trades = sum(1 for trade in trades if (trade.pnl or 0) > 0)
            losing_trades = sum(1 for trade in trades if (trade.pnl or 0) < 0)
            best_trade = max((trade.pnl or 0 for trade in trades), default=0)
            worst_trade = min((trade.pnl or 0 for trade in trades), default=0)

            win_rate = (winning_trades / len(trades) * 100) if trades else 0

            # Get portfolio value
            simulation = db.query(Simulation).first()
            portfolio_value = simulation.current_capital if simulation else 0

            # Placeholder for completion rate
            completion_rate = 75.0

            # Generate HTML
            html = EmailTemplate.daily_report_html(
                user_email,
                daily_pnl,
                win_rate,
                winning_trades,
                losing_trades,
                best_trade,
                worst_trade,
                len(trades),
                completion_rate,
                portfolio_value,
            )

            # Send email
            return await EmailService.send_email(
                user_email,
                f"Daily Trading Report - {today.strftime('%B %d, %Y')}",
                html,
            )
        except Exception as e:
            logger.error(f"Failed to send daily report: {str(e)}")
            return False

    @staticmethod
    async def send_weekly_report(
        db: Session,
        user_email: str,
    ) -> bool:
        """Send weekly trading report email"""
        try:
            from datetime import timedelta

            # Get this week's trades
            today = date.today()
            week_start = today - timedelta(days=today.weekday())
            week_end = week_start + timedelta(days=6)

            trades = db.query(Trade).filter(
                db.func.date(Trade.timestamp) >= week_start,
                db.func.date(Trade.timestamp) <= week_end
            ).all()

            # Calculate metrics
            weekly_pnl = sum(trade.pnl or 0 for trade in trades)
            winning_trades = sum(1 for trade in trades if (trade.pnl or 0) > 0)
            win_rate = (winning_trades / len(trades) * 100) if trades else 0
            best_trade = max((trade.pnl or 0 for trade in trades), default=0)
            largest_loss = min((trade.pnl or 0 for trade in trades), default=0)

            # Find best/worst trading day
            daily_pnls = {}
            for trade in trades:
                day = trade.timestamp.date()
                if day not in daily_pnls:
                    daily_pnls[day] = 0
                daily_pnls[day] += trade.pnl or 0

            best_day = max(daily_pnls, key=daily_pnls.get).strftime('%A') if daily_pnls else "N/A"
            worst_day = min(daily_pnls, key=daily_pnls.get).strftime('%A') if daily_pnls else "N/A"

            # Generate HTML
            html = EmailTemplate.weekly_report_html(
                user_email,
                weekly_pnl,
                len(trades),
                win_rate,
                best_day,
                worst_day,
                best_trade,
                largest_loss,
            )

            # Send email
            return await EmailService.send_email(
                user_email,
                f"Weekly Trading Summary - Week of {week_start.strftime('%B %d')}",
                html,
            )
        except Exception as e:
            logger.error(f"Failed to send weekly report: {str(e)}")
            return False
