<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[Fillable(['user_id', 'display_name', 'photo', 'bio', 'experience_years', 'aadhaar_verified', 'police_verified', 'is_online', 'is_available', 'suspend_new_bookings', 'suspend_reason', 'aadhaar_number', 'aadhaar_doc_path', 'police_doc_path', 'aadhaar_verified_at', 'police_verified_at', 'admin_notes', 'rating_avg', 'rating_count', 'jobs_done', 'working_hours', 'lat', 'lng', 'commission_percent', 'commission_notes', 'wallet_balance_paise', 'pending_balance_paise'])]
class PartnerProfile extends Model
{
    use HasFactory;

    protected $casts = [
        'aadhaar_verified' => 'boolean',
        'police_verified' => 'boolean',
        'is_online' => 'boolean',
        'is_available' => 'boolean',
        'suspend_new_bookings' => 'boolean',
        'aadhaar_verified_at' => 'datetime',
        'police_verified_at' => 'datetime',
        'rating_avg' => 'decimal:2',
        'rating_count' => 'integer',
        'jobs_done' => 'integer',
        'experience_years' => 'integer',
        'working_hours' => 'array',
        'commission_percent' => 'decimal:2',
        'wallet_balance_paise' => 'integer',
        'pending_balance_paise' => 'integer',
    ];

    public function isSuspended(): bool
    {
        return (bool) $this->suspend_new_bookings;
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function services(): BelongsToMany
    {
        return $this->belongsToMany(Service::class, 'partner_service', 'partner_id', 'service_id')
            ->withPivot('custom_price_paise', 'is_active');
    }

    public function areas(): BelongsToMany
    {
        return $this->belongsToMany(Area::class, 'partner_area', 'partner_id', 'area_id')
            ->withPivot('is_active');
    }

    public function bookings(): HasMany
    {
        return $this->hasMany(Booking::class, 'partner_id');
    }

    public function ratings(): HasMany
    {
        return $this->hasMany(Rating::class, 'partner_id');
    }

    public function teamMembers(): HasMany
    {
        return $this->hasMany(PartnerTeamMember::class, 'partner_id');
    }

    public function activeTeamMembers(): HasMany
    {
        return $this->hasMany(PartnerTeamMember::class, 'partner_id')->where('is_active', true);
    }

    public function getPhotoUrlAttribute(): ?string
    {
        if (!$this->photo) return null;
        if (str_starts_with($this->photo, 'http')) return $this->photo;
        return asset('storage/' . $this->photo);
    }

    public function initials(): string
    {
        $name = $this->display_name ?? $this->user?->name ?? '?';
        $parts = explode(' ', trim($name));
        if (count($parts) >= 2) {
            return strtoupper(substr($parts[0], 0, 1) . substr($parts[1], 0, 1));
        }
        return strtoupper(substr($name, 0, 2));
    }

    public function scopeApproved($q)
    {
        return $q->whereHas('user', fn($qq) => $qq->where('is_partner_approved', true));
    }

    public function scopeAvailable($q)
    {
        return $q->where('is_available', true);
    }
}
