"""
通知サービス
Slackなどへの通知を管理
"""
import requests


class NotificationService:
    """通知サービスクラス"""

    def __init__(self, slack_webhook_url: str = None):
        """
        初期化

        Args:
            slack_webhook_url: Slack Webhook URL
        """
        self.slack_webhook_url = slack_webhook_url

    def send_slack_error(self, error_message: str, script_name: str = None):
        """
        Slackにエラー通知を送信

        Args:
            error_message: エラーメッセージ
            script_name: スクリプト名（オプション）
        """
        if not self.slack_webhook_url:
            print("Slack Webhook URLが設定されていません")
            return

        try:
            title = f"🚨 エラー発生: {script_name}" if script_name else "🚨 エラー発生"
            payload = {
                "text": f"{title}\n```\n{error_message}\n```"
            }
            response = requests.post(
                self.slack_webhook_url,
                json=payload,
                timeout=10
            )
            response.raise_for_status()
        except Exception as e:
            print(f"Slack通知の送信に失敗しました: {e}")

    def send_slack_message(self, message: str):
        """
        Slackにメッセージを送信

        Args:
            message: 送信するメッセージ
        """
        if not self.slack_webhook_url:
            print("Slack Webhook URLが設定されていません")
            return

        try:
            payload = {"text": message}
            response = requests.post(
                self.slack_webhook_url,
                json=payload,
                timeout=10
            )
            response.raise_for_status()
        except Exception as e:
            print(f"Slack通知の送信に失敗しました: {e}")
