<?php

namespace App\Livewire\Member;

use App\Enums\PointRecordType;
use App\Models\Membership as MembershipModel;
use App\Models\PointRecord;
use App\Models\Tier;
use App\Models\TierReward;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Membership')]
#[Layout('components.layouts.member')]
class Membership extends Component
{
    public ?MembershipModel $membership = null;

    public array $stats = [];

    public string $nextTierName = '';

    public string $currentPoints = '';

    public string $nextTierPoints = '';

    public string $progress = '';

    public array $rewards = [];

    public array $pointRecords = [];

    public function mount()
    {
        $this->membership = MembershipModel::with(['tier', 'tier.rewards'])->where('user_id', auth()->id())->first();

        if (! $this->membership) {
            return;
        }

        $monthlyTierAddition = PointRecord::where('user_id', auth()->id())
            ->where('type', PointRecordType::TIER)
            ->where('is_addition', true)
            ->whereYear('created_at', now()->year)
            ->whereMonth('created_at', now()->month)
            ->sum('change');

        $nextTier = Tier::where('min_points', '>', $this->membership->tier_points)
            ->orderBy('min_points', 'asc')
            ->first();

        $this->nextTierName = $nextTier ? $nextTier->name : 'Maksimal';

        $this->currentPoints = $this->membership->tier_points;

        $this->nextTierPoints = $nextTier ? $nextTier->min_points : 'Maksimal';

        $this->progress = ($this->membership->tier_points / $nextTier->min_points) * 100;

        $this->stats = [
            [
                'title' => 'Poin Tier',
                'value' => currency($this->membership->tier_points),
                'description' => '+'.currency($monthlyTierAddition).' bulan ini',
            ],
            [
                'title' => 'Tier Sekarang',
                'value' => $this->membership->tier->name,
                'description' => 'Terdaftar sejak '.formatDate($this->membership->created_at),
            ],
            [
                'title' => 'Tier Selanjutnya',
                'value' => $nextTier ? $nextTier->name : 'Maksimal',
                'description' => $nextTier ? ($nextTier->min_points - $this->membership->tier_points).' poin lagi' : 'Anda sudah berada di tier tertinggi',
            ],
            [
                'title' => 'Poin Hadiah',
                'value' => currency($this->membership->reward_points),
                'description' => 'Nikmati hadiah sekarang',
            ],
        ];

        $this->rewards = TierReward::where('tier_id', $this->membership->tier_id)
            ->latest()
            ->get()
            ->map(fn ($reward) => [
                'name' => $reward->name,
                'value' => currency($reward->value, 'Rp'),
            ])
            ->toArray();

        $this->pointRecords = PointRecord::where('user_id', auth()->id())
            ->latest()
            ->limit(10)
            ->get()
            ->map(fn ($record) => [
                'date' => formatDate($record->created_at),
                'description' => $record->description,
                'change' => currency($record->change),
                'type' => $record->type->label(),
                'is_addition' => $record->is_addition,
            ])
            ->toArray();
    }

    public function render()
    {
        return view('livewire.member.memberships', [
            'pageTitle' => 'Membership',
        ]);
    }
}
