<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

final class InvestmentProperty extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'description',
        'property_type',
        'bedrooms',
        'bathrooms',
        'beds',
        'max_guests',
        'address',
        'city',
        'state',
        'country',
        'postal_code',
        'latitude',
        'longitude',
        'total_shares',
        'shares_available',
        'price_per_share',
        'minimum_shares',
        'maximum_shares',
        'expected_annual_return',
        'investment_status',
        'launch_date',
        'maturity_date',
        'property_images',
        'amenities',
        'market_analysis',
        'financial_projections',
        'google_drive_link',
    ];

    protected function casts(): array
    {
        return [
            'bedrooms' => 'integer',
            'bathrooms' => 'integer',
            'beds' => 'integer',
            'max_guests' => 'integer',
            'latitude' => 'decimal:7',
            'longitude' => 'decimal:7',
            'total_shares' => 'integer',
            'shares_available' => 'integer',
            'price_per_share' => 'decimal:2',
            'minimum_shares' => 'integer',
            'maximum_shares' => 'integer',
            'expected_annual_return' => 'decimal:2',
            'launch_date' => 'date',
            'maturity_date' => 'date',
            'property_images' => 'json',
            'amenities' => 'json',
            'market_analysis' => 'json',
            'financial_projections' => 'json',
            'google_drive_link' => 'string',
        ];
    }

    public function investments(): HasMany
    {
        return $this->hasMany(PartnerInvestment::class);
    }

    public function distributions(): HasMany
    {
        return $this->hasMany(InvestmentDistribution::class);
    }

    public function performance(): HasMany
    {
        return $this->hasMany(InvestmentPerformance::class);
    }

    /**
     * Scope to get only active properties
     */
    public function scopeActive($query)
    {
        return $query->where('investment_status', 'active');
    }

    /**
     * Get total invested amount
     */
    public function totalInvested(): float
    {
        return (float) $this->investments()->sum('total_investment');
    }

    /**
     * Get total number of investors
     */
    public function investorCount(): int
    {
        return $this->investments()->distinct('partner_id')->count('partner_id');
    }
}
