<?php

namespace App\Http\Controllers;

use App\Models\Person;

class PersonController extends Controller
{
    public function index()
    {
        $people = Person::query()
            ->orderBy('name_kana')
            ->orderBy('name')
            ->get();

        return view('people.index', compact('people'));
    }

    public function show(Person $person)
    {
        $person->load([
            'performanceRunPeople.performanceRun.performance',
            'performanceRunPeople.performanceWork.work',
        ]);

        $appearances = $person->performanceRunPeople
            ->sortBy([
                fn($item) => $item->performanceRun?->start_date,
                fn($item) => $item->credit_order,
            ]);

        return view('people.show', compact('person', 'appearances'));
    }
}
