<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class MexcoCampusApplication extends Model
{
    public const STATUS_SUBMITTED = 'submitted';

    public const STATUS_UNDER_REVIEW = 'under_review';

    public const STATUS_APPROVED = 'approved';

    public const STATUS_REJECTED = 'rejected';

    public const STATUS_EVENT_CREATED = 'event_created';

    public const STATUS_ACTIVE = 'active';

    public const STATUS_COMPLETED = 'completed';

    public const STATUS_RETENTION = 'retention';

    public const STATUS_DELETED = 'deleted';

    protected $table = 'mexco_campus_applications';

    protected $fillable = [
        'reference_no',
        'organisation_id',
        'organisation_user_id',
        'institution',
        'faculty',
        'student_organisation',
        'event_name',
        'event_date',
        'expected_participants',
        'sessions_count',
        'booths_count',
        'event_objective',
        'event_description',
        'contact_person',
        'contact_email',
        'applicant_phone',
        'student_id',
        'advisor_name',
        'advisor_email',
        'requested_sponsorship',
        'promotional_exposure',
        'promotional_commitments',
        'sponsorship_letter_path',
        'sponsorship_letter_name',
        'status',
        'sponsorship_value_cents',
        'reviewed_by',
        'reviewed_at',
        'review_notes',
        'event_id',
        'event_license_id',
        'metadata',
    ];

    protected function casts(): array
    {
        return [
            'event_date' => 'date',
            'reviewed_at' => 'datetime',
            'promotional_commitments' => 'array',
            'metadata' => 'array',
        ];
    }

    public function organisation(): BelongsTo
    {
        return $this->belongsTo(MexcoOrganisation::class, 'organisation_id');
    }

    public function organisationUser(): BelongsTo
    {
        return $this->belongsTo(MexcoOrganisationUser::class, 'organisation_user_id');
    }

    public function event(): BelongsTo
    {
        return $this->belongsTo(MeetupEvent::class, 'event_id');
    }

    public function license(): BelongsTo
    {
        return $this->belongsTo(MexcoEventLicense::class, 'event_license_id');
    }

    public function reviewer(): BelongsTo
    {
        return $this->belongsTo(MeetupSuperAdmin::class, 'reviewed_by');
    }

    public static function statuses(): array
    {
        return [
            self::STATUS_SUBMITTED => 'Submitted',
            self::STATUS_UNDER_REVIEW => 'Under review',
            self::STATUS_APPROVED => 'Approved',
            self::STATUS_REJECTED => 'Rejected',
            self::STATUS_EVENT_CREATED => 'Event created',
            self::STATUS_ACTIVE => 'Active',
            self::STATUS_COMPLETED => 'Completed',
            self::STATUS_RETENTION => 'Retention period',
            self::STATUS_DELETED => 'Deleted',
        ];
    }

    public function statusLabel(): string
    {
        return self::statuses()[$this->status] ?? ucfirst(str_replace('_', ' ', $this->status));
    }

    public function isAwaitingReview(): bool
    {
        return in_array($this->status, [
            self::STATUS_SUBMITTED,
            self::STATUS_UNDER_REVIEW,
        ], true);
    }

    public function isRejected(): bool
    {
        return $this->status === self::STATUS_REJECTED;
    }

    public function hasSponsorshipLetter(): bool
    {
        return filled($this->sponsorship_letter_path);
    }

    public function formattedSponsorshipValue(): string
    {
        if (! $this->sponsorship_value_cents) {
            return '-';
        }

        return 'MYR '.number_format($this->sponsorship_value_cents / 100, 0);
    }
}
