<?php

namespace Mehrab\City\Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Hash;
use Mehrab\City\Models\City;
use Mehrab\RolePermission\Database\Seeds\RolePermissionSeeder;
use Mehrab\RolePermission\Models\Permission;
use Mehrab\User\Models\User;
use Tests\TestCase;

class CityTest extends TestCase
{
    use RefreshDatabase;

    //permitted user can see city index
    public function test_permitted_user_can_see_city_index(){
        $this->actionAsAdmin();
        $this->get(route('city.index'))->assertStatus(200);
    }
    //normal user can not see city index
    public function test_normal_user_can_not_see_city_index(){
        $this->actionAsNormalUser();
        $this->get(route('city.index'))->assertStatus(403);
    }

    //permitted user can see city create
    public function test_permitted_user_can_see_city_create(){
        $this->actionAsAdmin();
        $this->get(route('city.create'))->assertStatus(200);
    }
    //normal user can not see city create
    public function test_normal_user_can_not_see_city_create(){
        $this->actionAsNormalUser();
        $this->get(route('city.create'))->assertStatus(403);
    }
    //permitted user can store city
    public function test_permitted_user_can_store_city_create(){
        $this->actionAsAdmin();
        $this->post(route('city.store'),[
            'name' => 'mm',
            'slug' => 'nnn',
            'priority' => 22,
        ])->assertStatus(302);
        $this->assertEquals(City::query()->count(),1);

    }
    //normal user can not city store
    public function test_normal_user_can_not_store_city_create(){
        $this->actionAsNormalUser();
        $this->post(route('city.store'),[
            'name' => 'mm',
            'slug' => 'nnn',
        ])->assertStatus(403);

        $this->assertEquals(City::query()->count(),0);

    }

    //permitted user can see city edit
    public function test_permitted_user_can_see_city_edit(){
        $this->actionAsAdmin();
        $city = City::create([
            'name' => 'mm',
            'slug' => 'nnn',
        ]);

        $this->get(route('city.edit',$city->id))->assertStatus(200);
    }
    //normal user can not see city edit
    public function test_normal_user_can_not_see_city_edit(){
        $this->actionAsNormalUser();
        $city = City::create([
            'name' => 'mm',
            'slug' => 'nnn',
        ]);
        $this->assertEquals(City::query()->count(),1);
        $this->get(route('city.edit',$city->id))->assertStatus(403);
    }
    //permitted user can city update
    public function test_permitted_user_can_city_update(){
        $this->actionAsAdmin();
        $city = City::query()->create([
            'name' => 'mm',
            'slug' => 'nnn',
        ]);
        $update = $this->patch(route('city.update',1),[
            'name' => 'mmo',
            'slug' => 'nn4n',
            'priority' => 6,
        ]);
        $city->refresh();
        $this->assertEquals('mmo',$city->name);
    }
    //normal user can not city update
    public function test_Normal_user_can_not_city_update(){
        $this->actionAsNormalUser();
        $city = City::create([
            'name' => 'mm',
            'slug' => 'nnn',
        ]);
        $this->put(route('city.update',$city->id),[
            'name' => 'mmo',
        ]);

        $city->refresh();
        $this->assertNotEquals('mmo',$city->name);

    }
    //permitted user can change status city
    public function test_permitted_user_can_city_change_status(){
        $this->actionAsAdmin();
        $city = City::query()->create([
            'name' => 'mm',
            'slug' => 'nnn',
        ]);
        $update = $this->put(route('city.status',$city->id),[]);
        $update->assertOk();
    }
    //normal user can not change status city
    public function test_Normal_user_can_not_city_change_status(){
        $this->actionAsNormalUser();
        $city = City::query()->create([
            'name' => 'mm',
            'slug' => 'nnn',
        ]);
        $update = $this->put(route('city.status',$city->id),[]);
        $update->assertStatus(403);
    }

    public function create_user()
    {
      $user = User::query()->create([
            'mobile' => '09358787887',
            'password' => Hash::make('123456Hb'),
            'mobile_verified_at' => time(),
        ]);
       auth()->login($user);
       return $user;
    }

    public function actionAsAdmin()
    {
        $user = $this->create_user();
        $this->seed(RolePermissionSeeder::class);
        $user->givePermissionTo(Permission::PERMISSION_MANAGE_STATE);
    }

    public function actionAsNormalUser()
    {
        $user = $this->create_user();
        $this->seed(RolePermissionSeeder::class);
    }
}
