<?php

namespace App\Services\Client;

use App\Models\MemberUser;
use App\Models\MemberSocialAccount;
use App\Models\SysConfig;
use App\Models\Wallet;
use App\Models\CoachUser;
use App\Models\WalletWithdrawRecord;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;


class UserService
{

    /**
     * 获取用户信息
     */
    public function getUserInfo()
    {
        $user = Auth::user();
        $wallet = Wallet::where('owner_id', $user->id)
            ->where('owner_type', 'USER')
            ->first();

        return [
            'user' => $user,
            'wallet' => $wallet ? [
                'id' => $wallet->id,
                'balance' => $wallet->available_balance
            ] : null
        ];
    }

    /**
     * 修改用户信息
     */
    public function updateUserInfo(array $data)
    {
        $user = Auth::user();
        $user->update($data);
        return ['message' => '修改成功'];
    }

    /**
     * 获取用户钱包
     */
    public function getUserWallet()
    {
        $wallet = Wallet::where('owner_id', Auth::id())
            ->where('owner_type', MemberUser::class)
            ->first();

        if (!$wallet) {
            throw new \Exception('钱包不存在');
        }

        return $wallet;
    }

    /**
     * 用户提现
     */
    public function withdraw($amount)
    {
        // 获取当前用户
        $user = Auth::user();
        if (!$user || $user->state !== 'enable') {
            throw new \Exception('用户状态异常');
        }
        
        $config = SysConfig::where('key', 'withdraw_min_amount')->first();
        $configValue = json_decode($config->value, true);
        $minAmount = $configValue['minAmount'];
        $maxAmount = $configValue['maxAmount'];
        $fee = $configValue['fee'];
        
        if ($amount < $minAmount) {
            throw new \Exception("提现金额不能小于{$minAmount}元");
        }
        
        if ($amount > $maxAmount) {
            throw new \Exception("提现金额不能大于{$maxAmount}元"); 
        }

        // 创建提现记录
        DB::transaction(function() use ($user, $amount, $fee) {
            WalletWithdrawRecord::create([
                'user_id' => $user->id,
                'amount' => $amount,
                'fee' => $fee,
                'type' => MemberUser::class,
                'state' => 'pending'
            ]);
        });

        return ['message' => '提现申请已提交'];
    }

    /**
     * 用户反馈
     */
    public function feedback($content)
    {
        $user = Auth::user();
        if (!$user || $user->state !== 'enable') {
            throw new \Exception('用户状态异常');
        }

        // UserFeedback::create([
        //     'owner_id' => $user->id,
        //     'owner_type' => MemberUser::class,
        //     'content' => $content
        // ]);

        return ['message' => '反馈已提交'];
    }

    /**
     * 申请成为技师
     */
    public function applyCoach()
    {
        // 获取当前用户
        $userId = Auth::id();
        $user = MemberUser::findOrFail($userId);
        
        // 检查用户状态
        if ($user->state !== 'enable') {
            throw new \Exception('用户状态异常');
        }
        
        // 检查是否已经是技师
        $exists = CoachUser::where('user_id', $userId)->exists();
        if ($exists) {
            throw new \Exception('您已经是技师了');
        }
        
        // 创建技师申请
        DB::transaction(function() use ($userId) {
            CoachUser::create([
                'user_id' => $userId,
                'state' => 'pending'  // 待审核状态
            ]);
        });
        
        return ['message' => '申请已提交'];
    }
} 