<?php

namespace App\Livewire\Companies;

use App\Models\Company;
use App\Models\Issue;
use App\Models\Project;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('layouts.app')]
class CompanyView extends Component
{
    public Company $company;

    public string $activeTab = 'summary';

    // Projects tab
    public ?int $addProjectId = null;

    public string $addProjectRole = '';

    public $availableProjects;

    // People tab
    public ?int $assignUserId = null;

    public $assignableUsers;

    // Notes tab
    public string $notes = '';

    public bool $editingNotes = false;

    // Stats (computed once in mount, refreshed on mutations)
    public int $usersCount = 0;

    public int $projectsCount = 0;

    public float $avgProgress = 0.0;

    public int $openIssues = 0;

    public function mount(Company $company): void
    {
        abort_unless(Auth::user()->can('view companies'), 403);

        $this->company = $company->load(['users.roles', 'projects.phases']);
        $this->notes = $company->notes ?? '';

        $this->loadAvailableProjects();
        $this->loadAssignableUsers();
        $this->computeStats();
    }

    // ── Helpers ───────────────────────────────────────────────────────────────

    private function loadAvailableProjects(): void
    {
        $assignedIds = $this->company->projects->pluck('id');
        $this->availableProjects = Project::whereNotIn('id', $assignedIds)
            ->orderBy('name')->get();
    }

    private function loadAssignableUsers(): void
    {
        $this->assignableUsers = User::where(function ($q) {
            $q->where('company_id', '!=', $this->company->id)
                ->orWhereNull('company_id');
        })->orderBy('name')->get();
    }

    private function computeStats(): void
    {
        $this->usersCount = $this->company->users->count();
        $this->projectsCount = $this->company->projects->count();
        $this->avgProgress = round(
            $this->company->projects->flatMap(fn ($p) => $p->phases)->avg('progress_percent') ?? 0
        );
        $userIds = $this->company->users->pluck('id');
        $this->openIssues = $userIds->isNotEmpty()
            ? Issue::whereIn('reported_by', $userIds)->where('status', 'open')->count()
            : 0;
    }

    // ── Tabs ─────────────────────────────────────────────────────────────────

    public function setTab(string $tab): void
    {
        $this->activeTab = $tab;
    }

    // ── Projects ──────────────────────────────────────────────────────────────

    public function assignProject(): void
    {
        $this->validate([
            'addProjectId' => 'required|exists:projects,id',
            'addProjectRole' => 'required|string|max:150',
        ], [], ['addProjectId' => 'proyecto', 'addProjectRole' => 'rol en proyecto']);

        $this->company->projects()->attach($this->addProjectId, [
            'role_in_project' => $this->addProjectRole,
        ]);

        $this->company->load('projects.phases');
        $this->addProjectId = null;
        $this->addProjectRole = '';
        $this->loadAvailableProjects();
        $this->computeStats();
        $this->dispatch('notify', 'Proyecto asignado correctamente.');
    }

    public function removeProject(int $projectId): void
    {
        $this->company->projects()->detach($projectId);
        $this->company->load('projects.phases');
        $this->loadAvailableProjects();
        $this->computeStats();
        $this->dispatch('notify', 'Proyecto desasignado.');
    }

    // ── People ────────────────────────────────────────────────────────────────

    public function assignUser(): void
    {
        $this->validate([
            'assignUserId' => 'required|exists:users,id',
        ], [], ['assignUserId' => 'usuario']);

        User::find($this->assignUserId)?->update(['company_id' => $this->company->id]);

        $this->company->load('users.roles');
        $this->assignUserId = null;
        $this->loadAssignableUsers();
        $this->computeStats();
        $this->dispatch('notify', 'Usuario vinculado a la empresa.');
    }

    public function removeUser(int $userId): void
    {
        User::find($userId)?->update(['company_id' => null]);
        $this->company->load('users.roles');
        $this->loadAssignableUsers();
        $this->computeStats();
        $this->dispatch('notify', 'Usuario desvinculado de la empresa.');
    }

    // ── Notes ─────────────────────────────────────────────────────────────────

    public function saveNotes(): void
    {
        $this->validate(['notes' => 'nullable|string']);
        $this->company->update(['notes' => $this->notes ?: null]);
        $this->editingNotes = false;
        $this->dispatch('notify', 'Notas guardadas.');
    }

    public function render()
    {
        return view('livewire.companies.company-view');
    }
}
