<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use App\Models\Product;
use App\Models\User;
use Illuminate\Support\Facades\URL;

class WinnerMail extends Mailable
{
    use Queueable, SerializesModels;

    public $product;
    public $user;

    /**
     * Create a new message instance.
     */
    public function __construct(Product $product, User $user)
    {
        $this->product = $product;
        $this->user = $user;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: '🎉 Congratulations! You won the auction',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        $environment_data = getEnvironmentData();
        $payment_options = $environment_data['PAYMENT_OPTIONS'];
        $codEnabled = data_get($payment_options, 'value', 0);
        $PAYMENT_DEADLINE_FOR_AUCTION_WINNER_DAYS = $environment_data['PAYMENT_DEADLINE_FOR_AUCTION_WINNER_DAYS'];

        if($codEnabled == 1){
            $payment_link = URL::temporarySignedRoute(
                'admin.payment.select_payment_option', // named route
                now()->addDays($PAYMENT_DEADLINE_FOR_AUCTION_WINNER_DAYS)->endOfDay(), // 15th day 11:59 PM
                [
                    'user_id' => $this->user->id,
                    'product_sku' => $this->product->sku,
                ]
            );
        } else {
            $payment_link = URL::temporarySignedRoute(
                'admin.payment.process_to_buy_product', // named route
                now()->addDays($PAYMENT_DEADLINE_FOR_AUCTION_WINNER_DAYS)->endOfDay(), // 15th day 11:59 PM
                [
                    'user_id' => $this->user->id,
                    'product_sku' => $this->product->sku,
                ]
            );  
        }

        return new Content(
            view: 'emails.winner',
            with: [
                'name' => $this->user->first_name.' '.$this->user->last_name,
                'product_name' => $this->product->name,
                'payment_link' => $payment_link,
                'hours_valid' => $environment_data['PAYMENT_DEADLINE_FOR_AUCTION_WINNER_DAYS'],
                // 'payment_link' => "https://torathy.com/admin/process-to-buy-product/$this->user->id/$this->product->sku"
            ],
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}
