<?php

namespace Yas\Member\Mail;

use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
//use Yas\Member\Contracts\Member;
use Yas\Member\Models\Member;
use Yas\FrontTheme\Mail\Mailable;
use Illuminate\Support\Facades\DB;
//use Yas\Member\Repositories\ConsignmentEnquiryRepository;
use Yas\Member\Models\ConsignmentEnquiry;
use Webkul\Attribute\Repositories\AttributeOptionRepository;
use Webkul\Attribute\Repositories\AttributeRepository;

class ConsignmentFormSubmitNotification extends Mailable
{
    /**
     * Create a new mailable instance.
     *
     * @return void
     */
    public function __construct(public Member $member,public ConsignmentEnquiry $consignment) {}

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            to: [
                new Address($this->member->email),
            ],
            subject: trans('member::app.member.emails.consignment.subject'),
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        $brandLabel = null;

        if ($this->consignment->brand) {
            $brandOption = app(AttributeOptionRepository::class)
                ->find($this->consignment->brand);

            $brandLabel = $brandOption?->admin_name ?? $brandOption?->label;
        }
        $this->consignment->brand_label = $brandLabel;

        $attributeGroupId = DB::table('attribute_groups')
        ->where('code', 'custom')
        ->value('id');
        
        $attributeIds = DB::table('attribute_group_mappings')
        ->where('attribute_group_id', $attributeGroupId)
        ->pluck('attribute_id')
        ->toArray();
        $attributes = app(AttributeRepository::class)->whereIn('id',$attributeIds) ->pluck('admin_name', 'code')
        ->toArray();

        return new Content(
            view: 'yas_member::emails.member.consignment-form-submit',
            with: [
                'attributes' => $attributes, // Explicitly pass the attributes map
                'member'=>$this->member,
                'consignment' => $this->consignment,
            ],
        );
    }
}
