<?php

namespace App\Models;

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

class C2SeedlabBeneficiary extends Model
{
    use SoftDeletes;

    protected $table = 'c2_seedlab_beneficiaries';

    protected $guarded = ['id'];

    protected $casts = [
        'age'                      => 'integer',
        'sn'                       => 'integer',
        'cohort'                   => 'string',
        'cohort_year'              => 'integer',
        'woef_connect_participation' => 'integer',
        'bootcamp_module_1'        => 'string',
        'bootcamp_module_2'        => 'string',
        'bootcamp_module_3'        => 'string',
        'bootcamp_module_4'        => 'string',
        'bootcamp_module_5'        => 'string',
        'pitch_presentation'       => 'string',
        'pitch_outcome'            => 'string',
        'accelerator_module_1'     => 'string',
        'accelerator_module_2'     => 'string',
        'accelerator_module_3'     => 'string',
        'accelerator_module_4'     => 'string',
        'accelerator_module_5'     => 'string',
        'accelerator_module_6'     => 'string',
        'accelerator_module_7'     => 'string',
        'accelerator_module_8'     => 'string',
        'accelerator_module_9'     => 'string',
        'accelerator_module_10'    => 'string',
    ];

    /**
     * Optional link back to a VSLA group from Component 1
     */
    public function group(): BelongsTo
    {
        return $this->belongsTo(C1Group::class, 'group_id');
    }

    /**
     * Polymorphic one-to-one relationship with grants
     */
    public function grant(): MorphOne
    {
        return $this->morphOne(C2Grant::class, 'grantable');
    }

    // ────────────────────────────────────────────────
    // Bootcamp Helpers
    // ────────────────────────────────────────────────

    public function getBootcampCompletedCountAttribute(): int
    {
        $modules = [
            $this->bootcamp_module_1,
            $this->bootcamp_module_2,
            $this->bootcamp_module_3,
            $this->bootcamp_module_4,
            $this->bootcamp_module_5,
        ];

        return collect($modules)->filter(fn($status) => $status === 'Completed')->count();
    }

    public function getBootcampIsFullyCompletedAttribute(): bool
    {
        return $this->bootcamp_completed_count === 5;
    }

    // ────────────────────────────────────────────────
    // Pitch Helpers
    // ────────────────────────────────────────────────

    public function getPitchIsSuccessfulAttribute(): ?bool
    {
        if ($this->pitch_presentation !== 'Completed') {
            return null;
        }

        return $this->pitch_outcome === 'Passed';
    }

    // ────────────────────────────────────────────────
    // Accelerator Helpers
    // ────────────────────────────────────────────────

    public function getAcceleratorPassedCountAttribute(): int
    {
        $modules = [
            $this->accelerator_module_1,
            $this->accelerator_module_2,
            $this->accelerator_module_3,
            $this->accelerator_module_4,
            $this->accelerator_module_5,
            $this->accelerator_module_6,
            $this->accelerator_module_7,
            $this->accelerator_module_8,
            $this->accelerator_module_9,
            $this->accelerator_module_10,
        ];

        return collect($modules)
            ->filter(fn($status) => $status === 'Passed')
            ->count();
    }

    public function getAcceleratorIsFullyCompletedAttribute(): bool
    {
        // Only consider it fully completed if all 10 are set and passed
        $modules = [
            $this->accelerator_module_1, $this->accelerator_module_2, $this->accelerator_module_3,
            $this->accelerator_module_4, $this->accelerator_module_5, $this->accelerator_module_6,
            $this->accelerator_module_7, $this->accelerator_module_8, $this->accelerator_module_9,
            $this->accelerator_module_10,
        ];

        return !in_array(null, $modules, true) && $this->accelerator_passed_count === 10;
    }
}