<?php

namespace App\Mail;

use App\Order;
use App\Mail\Traits\UsesEmailTemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class PaymentMethodChangedUser extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels, UsesEmailTemplate;

    public $order;
    public $newPaymentMethod;
    public $paymentLink;

    public function __construct(Order $order, $newPaymentMethod, $paymentLink = null)
    {
        $this->onConnection(config('queue.default'));
        $this->onQueue(config('queue.connections.redis.queue'));

        $this->order = $order;
        $this->newPaymentMethod = $newPaymentMethod;
        $this->paymentLink = $paymentLink;

        $delivery = $order->delivery ? $order->delivery->first() : null;
        $fullName = $delivery ? ($delivery->last_name . ' ' . $delivery->first_name) : '';

        $paymentChangeDetailHtml = $this->buildPaymentChangeDetailHtml($order, $newPaymentMethod, $paymentLink);

        $this->loadTemplate('u_payment_method_changed', [
            'full_name'             => $fullName,
            'order_code'            => $order->code,
            'total_price'           => number_format($order->total_price),
            'payment_change_detail' => $paymentChangeDetailHtml,
        ]);
    }

    public function build()
    {
        return $this->buildFromTemplate();
    }

    protected function buildPaymentChangeDetailHtml(Order $order, $newPaymentMethod, $paymentLink): string
    {
        $methodText = ($newPaymentMethod == 0) ? 'クレジットカード決済' : '銀行振込';

        $html = '<div style="background-color: #f8f9fa; padding: 15px; border-radius: 5px; margin: 20px 0;">'
            . '<h3 style="margin-top: 0; color: #2c5aa0;">ご注文情報</h3>'
            . '<p><strong>注文番号:</strong> ' . $order->code . '</p>'
            . '<p><strong>合計金額:</strong> ¥' . number_format($order->total_price) . '</p>'
            . '<p><strong>お支払い方法:</strong> ' . $methodText . '</p>'
            . '</div>';

        if ($newPaymentMethod == 0 && $paymentLink) {
            $html .= '<div style="background-color: #fff3cd; padding: 15px; border-radius: 5px; border-left: 4px solid #ffc107; margin: 20px 0;">'
                . '<h3 style="margin-top: 0; color: #856404;">クレジットカード決済のお手続き</h3>'
                . '<p>下記のリンクをクリックして、クレジットカード情報をご入力ください。</p>'
                . '<div style="text-align: center; margin: 20px 0;">'
                . '<a href="' . $paymentLink . '" style="display: inline-block; background-color: #dc3545; color: white; padding: 12px 30px; text-decoration: none; border-radius: 5px; font-weight: bold;">クレジットカード決済を行う</a>'
                . '</div>'
                . '<p style="font-size: 12px; color: #666;">※このリンクは24時間有効です。<br>※お支払い完了後、自動的に決済状況が更新されます。</p>'
                . '</div>';
        } elseif ($newPaymentMethod == 1) {
            $html .= '<div style="background-color: #d1ecf1; padding: 15px; border-radius: 5px; border-left: 4px solid #0c5460; margin: 20px 0;">'
                . '<h3 style="margin-top: 0; color: #0c5460;">銀行振込のお手続き</h3>'
                . '<p>下記の口座へお振込みください。</p>'
                . '<div style="background-color: white; padding: 15px; border-radius: 3px; margin: 15px 0;">'
                . '<p><strong>振込先口座</strong></p>'
                . '<p>銀行名: 楽天銀行<br>支店番号: 253 第三営業支店<br>預金科目: 普通預金<br>口座番号: 7489335<br>口座名義: 株式会社もろふじ キレイ</p>'
                . '</div>'
                . '</div>';
        }

        return $html;
    }
}
