<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Productcolor extends Model
{
    use HasFactory, SoftDeletes;
    protected $table = 'product_color';
    protected $fillable = [
        'id',
        'product_id',
        'price',
        'pc_qty',
        'color_image',
        'code',
        'product_color',
        'discount_amount',
        'status',
        'recycling_points',
        'created_at',
        'updated_at',
    ];

    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    public function getColorNameAttribute()
    {
        $colorMap = [
            '#A78B8D' => 'Marblepink',
            '#80355C' => 'Marblered',
            '#61A8F8' => 'Babyblue',
            '#C55C82' => 'Pink',
            '#FFFFFF' => 'Purewhite',
            '#FF0000' => 'Red',
            '#CAD3D0' => 'Clear',
            '#BA9978' => 'Beige',
            '#88DB86' => 'Applegreen',
            '#DEE4EE' => 'Marblewhite',
            '#BDB8EE' => 'Marbleviolet',
            '#E9CBB9' => 'Marbleorange',
            '#FEF250' => 'Lemonyellow', 
            '#f4c2c2' => 'Babypink',
            '#D2C77D' => 'Metallicgold',
            '#DAD9C6' => 'Buttermilk',
            '#E9C500' => 'Yellow',
            '#D3CBC4' => 'Graybeige', 
            '#0F2D8D' => 'Blue',
            '#005E3A' => 'Green',
            '#58C3C2' => 'Marblegreen',
            '#BF8BFF' => 'Violet',
            '#6B4B36' => 'Brown',
            '#C0C0C0' => 'Silver',
            '#808080' => 'Grey',
            '#6CD2FA' => 'Skyblue',
            '#D09305' => 'Orange',
            '#13598B' => 'Metallicblue',
            '#B4B4B8' => 'Metallicsilver',
            '#9582BB' => 'Metalicviolet',
            '#A286F4' => 'Purple',
            '#62ADE6' => 'Marbleblue', 
            '#2FCBD4' => 'Avocadogreen',
            '#FFA07A' => 'Lightorange',
            '#DDD3D1' => 'Marblebeige', 
            '#F4C430' => 'Saffron',
            '#000000' => 'Black',
            '#D4AF37' => 'Marblegold',
            '#3D7764' => 'Metalicgreen',
            '#F8B700' => 'Metalicyellow',
            '#6F4E37' => 'Coffee',
            '#FFFDD0' => 'Cream',
            '#A9A9A9' => 'Darkgray',
            '#006A4E' => 'Bottlegreen',
        ];

        return $colorMap[strtoupper($this->product_color)] ?? $this->product_color;
    }

      protected static function booted()
    {
        static::deleting(function ($productColor) {
            // Delete related cart records
            Cart::where('productid', $productColor->product_id)
                ->where('color', $productColor->product_color)
                ->delete();

            // Delete related wishlist records
            Wishlist::where('product_id', $productColor->product_id)->delete();
        });
    }
}
