<?php

namespace App\Http\Controllers\Main;

use App\City;
use App\Country;
use App\Friend;
use App\Http\Controllers\Controller;
use App\Month;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Main\FriendsController;

class PagesController extends Controller
{
    public function index()
    {
        $user = Auth::user();

        return redirect()->to('/u/' . $user->username);
    }

    public function profile($username, Request $request)
    {
        if (!User::where('username', $username)->first()) return abort(404);

        $user = Auth::user();

        if ($user && $user->username === $username) {
            return view('main.profile', [
                "user" => $user
            ]);
        } elseif ($user) {
            // другие пользователи
        }
    }

    public function edit_profile()
    {
        $user = Auth::user();
        $dob = explode(' ', $user->dob);
        return view('main.edit', [
            "user" => $user,
            "dob" => (object)[
                "day" => $dob[0],
                "month" => Month::where('title', $dob[1])->first(),
                "year" => $dob[2]
            ],
            "months" => Month::all(),
            "countries" => Country::all(),
            "cities" => City::where("country_id", $user->country_id)->get(),
            "year" => [
                "min" => date('Y') - 90,
                "max" => date('Y') - 14
            ]
        ]);
    }

    public function friends()
    {
        return view('main.friends', [
            "friends" => FriendsController::myFriends()
        ]);
    }
}
