import 'dart:convert';
import 'package:flutter/foundation.dart';

/// 自社情報を管理するモデル
/// 請求書などに記載される自社の正式名称、住所、連絡先など
class Company {
  final String id; // ローカル管理用ID (シングルトンなので固定)
  final String formalName; // 正式名称 (例: 株式会社 〇〇)
  final String? representative; // 代表者名
  final String? zipCode; // 郵便番号
  final String? address; // 住所
  final String? tel; // 電話番号
  final String? fax; // FAX番号
  final String? email; // メールアドレス
  final String? website; // ウェブサイト
  final String? registrationNumber; // 登録番号 (インボイス制度対応)
  final String? notes; // 備考

  const Company({
    required this.id,
    required this.formalName,
    this.representative,
    this.zipCode,
    this.address,
    this.tel,
    this.fax,
    this.email,
    this.website,
    this.registrationNumber,
    this.notes,
  });

  /// 状態更新のためのコピーメソッド
  Company copyWith({
    String? id,
    String? formalName,
    String? representative,
    String? zipCode,
    String? address,
    String? tel,
    String? fax,
    String? email,
    String? website,
    String? registrationNumber,
    String? notes,
  }) {
    return Company(
      id: id ?? this.id,
      formalName: formalName ?? this.formalName,
      representative: representative ?? this.representative,
      zipCode: zipCode ?? this.zipCode,
      address: address ?? this.address,
      tel: tel ?? this.tel,
      fax: fax ?? this.fax,
      email: email ?? this.email,
      website: website ?? this.website,
      registrationNumber: registrationNumber ?? this.registrationNumber,
      notes: notes ?? this.notes,
    );
  }

  /// JSON変換 (ローカル保存用)
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'formal_name': formalName,
      'representative': representative,
      'zip_code': zipCode,
      'address': address,
      'tel': tel,
      'fax': fax,
      'email': email,
      'website': website,
      'registration_number': registrationNumber,
      'notes': notes,
    };
  }

  /// JSONからモデルを生成
  factory Company.fromJson(Map<String, dynamic> json) {
    return Company(
      id: json['id'] as String,
      formalName: json['formal_name'] as String,
      representative: json['representative'] as String?,
      zipCode: json['zip_code'] as String?,
      address: json['address'] as String?,
      tel: json['tel'] as String?,
      fax: json['fax'] as String?,
      email: json['email'] as String?,
      website: json['website'] as String?,
      registrationNumber: json['registration_number'] as String?,
      notes: json['notes'] as String?,
    );
  }

  // 初期データ (シングルトン的に利用)
  static const Company defaultCompany = Company(
    id: 'my_company',
    formalName: '自社名が入ります',
    zipCode: '〒000-0000',
    address: '住所がここに入ります',
    tel: 'TEL: 00-0000-0000',
    registrationNumber: '適格請求書発行事業者登録番号 T1234567890123', // インボイス制度対応例
    notes: 'いつもお世話になっております。',
  );
}
