<?php

namespace App\Livewire\Team;

use App\Models\Team;
use App\Models\TeamInvitation;
use App\Support\ValidationPatterns;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

class Index extends Component
{
    use AuthorizesRequests;

    public $invitations = [];

    public Team $team;

    // Explicit properties
    public string $name;

    public ?string $description = null;

    public bool $is_mcp_server_enabled = true;

    protected function rules(): array
    {
        return [
            'name' => ValidationPatterns::nameRules(),
            'description' => ValidationPatterns::descriptionRules(),
            'is_mcp_server_enabled' => 'boolean',
        ];
    }

    protected function messages(): array
    {
        return array_merge(
            ValidationPatterns::combinedMessages(),
            [
                'name.required' => 'The Name field is required.',
            ]
        );
    }

    protected $validationAttributes = [
        'name' => 'name',
        'description' => 'description',
    ];

    /**
     * Sync data between component properties and model
     *
     * @param  bool  $toModel  If true, sync FROM properties TO model. If false, sync FROM model TO properties.
     */
    private function syncData(bool $toModel = false): void
    {
        if ($toModel) {
            // Sync TO model (before save)
            $this->team->name = $this->name;
            $this->team->description = $this->description;
            $this->team->is_mcp_server_enabled = $this->is_mcp_server_enabled;
        } else {
            // Sync FROM model (on load/refresh)
            $this->name = $this->team->name;
            $this->description = $this->team->description;
            $this->is_mcp_server_enabled = $this->team->is_mcp_server_enabled;
        }
    }

    public function mount()
    {
        $this->team = currentTeam();
        $this->syncData(false);

        if (auth()->user()->isAdminFromSession()) {
            $this->invitations = TeamInvitation::whereTeamId(currentTeam()->id)->get();
        }
    }

    public function render()
    {
        return view('livewire.team.index');
    }

    public function submit()
    {
        $this->validate();
        try {
            $this->authorize('update', $this->team);
            $this->syncData(true);
            $this->team->save();
            refreshSession();
            $this->dispatch('success', 'Team updated.');
        } catch (\Throwable $e) {
            return handleError($e, $this);
        }
    }

    public function delete()
    {
        try {
            $currentTeam = currentTeam();
            $this->authorize('delete', $currentTeam);
            $currentTeam->members->each(function ($user) use ($currentTeam) {
                if ($user->id === Auth::id()) {
                    return;
                }
                $user->teams()->detach($currentTeam);
                $session = DB::table('sessions')->where('user_id', $user->id)->first();
                if ($session) {
                    DB::table('sessions')->where('id', $session->id)->delete();
                }
            });

            // Clear stale cache before deleting so refreshSession doesn't resolve the deleted team
            Cache::forget('user:'.Auth::id().':team:'.$currentTeam->id);
            $currentTeam->delete();

            // Switch to the user's next available team
            $newTeam = Auth::user()->teams()->first();
            refreshSession($newTeam);

            return redirect()->route('team.index');
        } catch (\Throwable $e) {
            return handleError($e, $this);
        }
    }
}
