<?php

namespace Tests\Tenant;

use Mtc\MercuryDataModels\ServicePackage;
use Tests\TenantTestCase;

class ServicePackageModelTest extends TenantTestCase
{
    public function testCreatingWithoutTypeDefaultsToPackage()
    {
        $package = ServicePackage::create([
            'name' => 'Test Package',
        ]);

        $this->assertEquals('package', $package->type);
    }

    public function testCreatingWithExplicitTypePreservesIt()
    {
        $package = ServicePackage::create([
            'name' => 'Test Add-on',
            'type' => 'add-on',
        ]);

        $this->assertEquals('add-on', $package->type);
    }

    public function testCreatingWithExplicitPackageTypePreservesIt()
    {
        $package = ServicePackage::create([
            'name' => 'Explicit Package',
            'type' => 'package',
        ]);

        $this->assertEquals('package', $package->type);
    }

    public function testSetSortByDefaultOrdersByOrderColumn()
    {
        ServicePackage::create(['name' => 'C', 'type' => 'package', 'order' => 2]);
        ServicePackage::create(['name' => 'A', 'type' => 'package', 'order' => 0]);
        ServicePackage::create(['name' => 'B', 'type' => 'package', 'order' => 1]);

        $packages = ServicePackage::query()->setSortBy(null)->get();

        $this->assertEquals(['A', 'B', 'C'], $packages->pluck('name')->toArray());
    }

    public function testSetSortByExplicitOrderColumn()
    {
        ServicePackage::create(['name' => 'C', 'type' => 'package', 'order' => 2]);
        ServicePackage::create(['name' => 'A', 'type' => 'package', 'order' => 0]);
        ServicePackage::create(['name' => 'B', 'type' => 'package', 'order' => 1]);

        $packages = ServicePackage::query()->setSortBy('order')->get();

        $this->assertEquals(['A', 'B', 'C'], $packages->pluck('name')->toArray());
    }

    public function testSetSortByNameAscending()
    {
        ServicePackage::create(['name' => 'Charlie', 'type' => 'package', 'order' => 0]);
        ServicePackage::create(['name' => 'Alpha', 'type' => 'package', 'order' => 1]);
        ServicePackage::create(['name' => 'Bravo', 'type' => 'package', 'order' => 2]);

        $packages = ServicePackage::query()->setSortBy('name')->get();

        $this->assertEquals(['Alpha', 'Bravo', 'Charlie'], $packages->pluck('name')->toArray());
    }

    public function testSetSortByNameDescending()
    {
        ServicePackage::create(['name' => 'Charlie', 'type' => 'package', 'order' => 0]);
        ServicePackage::create(['name' => 'Alpha', 'type' => 'package', 'order' => 1]);
        ServicePackage::create(['name' => 'Bravo', 'type' => 'package', 'order' => 2]);

        $packages = ServicePackage::query()->setSortBy('name_desc')->get();

        $this->assertEquals(['Charlie', 'Bravo', 'Alpha'], $packages->pluck('name')->toArray());
    }

    public function testSetSortByPriceAscending()
    {
        ServicePackage::create(['name' => 'Expensive', 'type' => 'package', 'price' => 300]);
        ServicePackage::create(['name' => 'Cheap', 'type' => 'package', 'price' => 50]);
        ServicePackage::create(['name' => 'Medium', 'type' => 'package', 'price' => 150]);

        $packages = ServicePackage::query()->setSortBy('price')->get();

        $this->assertEquals(['Cheap', 'Medium', 'Expensive'], $packages->pluck('name')->toArray());
    }

    public function testFillableFieldsAreMassAssignable()
    {
        $package = ServicePackage::create([
            'name' => 'Full Service',
            'type' => 'package',
            'price' => 199.99,
            'description' => 'Complete vehicle service',
            'order' => 5,
            'applicable_make_id' => 10,
            'applicable_model_id' => 20,
        ]);

        $this->assertEquals('Full Service', $package->name);
        $this->assertEquals('package', $package->type);
        $this->assertEquals(199.99, $package->price);
        $this->assertEquals('Complete vehicle service', $package->description);
        $this->assertEquals(5, $package->order);
        $this->assertEquals(10, $package->applicable_make_id);
        $this->assertEquals(20, $package->applicable_model_id);
    }
}
