<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class AdminBillingBlockNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $billing;
    protected $user;
    protected $type;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($billing, $user, $type)
    {
        $this->billing = $billing;
        $this->user = $user;
        $this->type = $type;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail', 'database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $title = "User Action Blocked: Pending Billing";
        
        return (new MailMessage)
            ->view('mail.general', [
                'title' => $title, 
                'content' => $this->getEmailContent()
            ])
            ->subject($title);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        $typeAction = $this->type === 'withdrawal' ? 'withdraw funds' : 'make an investment';
        
        return [
            'title' => 'User Action Blocked',
            'content' => $this->user->name . ' attempted to ' . $typeAction . ' but was blocked due to a pending billing of $' . $this->billing->amount,
            'url' => '/admin/billings'
        ];
    }

    /**
     * Get the email content
     * 
     * @return string
     */
    private function getEmailContent()
    {
        $typeAction = $this->type === 'withdrawal' ? 'withdraw funds' : 'make an investment';
        
        $content = '<p>A user attempted to ' . $typeAction . ' but was blocked due to a pending billing.</p>';
        $content .= '<p><strong>User Details:</strong></p>';
        $content .= '<ul>';
        $content .= '<li>Name: ' . $this->user->name . '</li>';
        $content .= '<li>Email: ' . $this->user->email . '</li>';
        $content .= '</ul>';
        
        $content .= '<p><strong>Billing Details:</strong></p>';
        $content .= '<ul>';
        $content .= '<li>Title: ' . $this->billing->title . '</li>';
        $content .= '<li>Amount: $' . $this->billing->amount . '</li>';
        $content .= '<li>Type: ' . ucfirst($this->billing->type) . '</li>';
        $content .= '<li>Description: ' . $this->billing->description . '</li>';
        $content .= '<li>Created: ' . $this->billing->created_at->format('Y-m-d H:i:s') . '</li>';
        $content .= '</ul>';
        
        $content .= '<p>This billing needs to be cleared before the user can proceed with ' . $this->type . ' operations.</p>';
        $content .= '<p>You may need to contact the user or take appropriate action.</p>';
        
        return $content;
    }
} 