<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Organization Model
 *
 * Represents an organization with its basic details including contact information,
 * location, formation year, and social media links. This is the central entity
 * for the Organization Configuration Module.
 *
 * @property int $id
 * @property string $name
 * @property string $short_name
 * @property string $country
 * @property string $city
 * @property string $postal_address
 * @property string $postal_code
 * @property string $physical_address
 * @property string $email
 * @property string $hotline
 * @property int $year_of_formation
 * @property array|null $social_media_links
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 */
class Organization extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'short_name',
        'country',
        'city',
        'postal_address',
        'postal_code',
        'physical_address',
        'email',
        'hotline',
        'year_of_formation',
        'social_media_links',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'social_media_links' => 'array',
        'year_of_formation' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * Get the leadership positions for the organization.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function leadershipPositions()
    {
        return $this->hasMany(LeadershipPosition::class);
    }

    /**
     * Get the election cycles for the organization.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function electionCycles()
    {
        return $this->hasMany(ElectionCycle::class);
    }

    /**
     * Get the voting procedures for the organization.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function votingProcedures()
    {
        return $this->hasMany(VotingProcedure::class);
    }

    /**
     * Get the leadership history records for the organization.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function leadershipHistories()
    {
        return $this->hasMany(LeadershipHistory::class);
    }

    /**
     * Get the staff history records for the organization.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function staffHistories()
    {
        return $this->hasMany(StaffHistory::class);
    }

    /**
     * Get the root leadership positions (positions without parent).
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function rootLeadershipPositions()
    {
        return $this->hasMany(LeadershipPosition::class)->whereNull('parent_position_id');
    }

    /**
     * Get the current leadership assignments for the organization.
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getCurrentLeadershipAssignments()
    {
        return LeadershipAssignment::whereHas('leadershipPosition', function ($query) {
            $query->where('organization_id', $this->id);
        })->where('is_current', true)->with(['user', 'leadershipPosition'])->get();
    }

    /**
     * Get the full address as a formatted string.
     *
     * @return string
     */
    public function getFullAddressAttribute(): string
    {
        return "{$this->physical_address}, {$this->city}, {$this->country}";
    }

    /**
     * Get the social media link for a specific platform.
     *
     * @param string $platform
     * @return string|null
     */
    public function getSocialMediaLink(string $platform): ?string
    {
        return $this->social_media_links[$platform] ?? null;
    }

    /**
     * Get the age of the organization in years.
     *
     * @return int
     */
    public function getAgeAttribute(): int
    {
        return now()->year - $this->year_of_formation;
    }
}
