<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Spatie\Translatable\HasTranslations;

class Project extends Model
{
    use HasTranslations, SoftDeletes;
    protected $guarded = ['id'];

    protected $translatable = ["name", "sub_name", "summaries", "img_alt", "address", "floor_plan",
        "building_structure", "design_unit", "contact_unit", "contact_phone", "inversment_phone", "district"
    ];
    protected $casts = ["img_url" => "array"];

    protected $appends = ["first_list_img_url", "img_list"];

    public function region()
    {
        return $this->belongsTo(Region::class);
    }

    public function tags()
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }

    public function badges()
    {
        return $this->morphToMany(Badge::class, 'badgeable')
        ->withPivot('award_date', 'award_type')  // ✅ 關鍵
        ->withTimestamps();
    }

    /**
     * 永續目標
     */
    public function targetBadges()
    {
        return $this->morphToMany(Badge::class, 'badgeable')
            ->withPivot('award_type')
            ->wherePivot('award_type', 1)
            ->withTimestamps();
    }

    /**
     * 取得標章
     */
    public function awardBadges()
    {
        return $this->morphToMany(Badge::class, 'badgeable')
            ->withPivot('award_type', 'award_date')
            ->wherePivot('award_type', 2)
            ->withTimestamps();
    }

    public function histories()
    {
        return $this->hasMany(ProjectHistory::class);
    }

    public function spaceInfos()
    {
        return $this->hasMany(ProjectSpaceInfo::class);
    }

    public function firstListImgUrl(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => !empty($this->img_url) ? Storage::url($this->img_url[0]) : null,
        );
    }

    public function imgList(): Attribute
    {
        $imgList = [];
        if(!is_null($this->img_url) && count($this->img_url) > 0){
            foreach($this->img_url as $img){
                $imgList[] = Storage::url($img);
            }
        }
        return Attribute::make(
            get: fn ($value) => $imgList,
        );
    }

    public function getBadges($locale) : array
    {
        $badges = [];
        if($this->badges->count() > 0){
            foreach($this->badges as $badge){
                $badges[] = [
                    "imgUrl" => $badge->imgUrlLink,
                    "name" => $badge->getTranslation("title", $locale),
                    "awardDate" => "取得時間 : " . date("Y.m", strtotime($badge->pivot->award_date))
                ];
            }
        }

        return $badges;
    }

    public function getBadgesByType($locale, $badgeType) : array
    {
        $badges = [];
        switch($badgeType){
            case "1":
                $badgesData = $this->targetBadges;
                foreach($badgesData as $badge){
                    $badges[] = [
                        "imgUrl" => $badge->imgUrlLink,
                        "name" => null,
                        "awardDate" => null
                    ];
                }
                break;
            case "2":
                $badgesData = $this->awardBadges;
                foreach($badgesData as $badge){
                    $badges[] = [
                        "imgUrl" => $badge->imgUrlLink,
                        "name" => $badge->getTranslation("title", $locale),
                        "awardDate" => "取得時間 : " . date("Y.m", strtotime($badge->pivot->award_date))
                    ];
                }
                break;
        }
        return $badges;
    }

    public function getTags($locale) : array
    {
        $tags = [];
        if($this->tags->count() > 0){
            foreach($this->tags as $tag){
                $tags[] = [
                    "id" => $tag->id,
                    "name" => $tag->getTranslation("name", $locale),
                ];
            }
        }

        return $tags;
    }
}
